Open qlaffont opened 1 year ago
As it seems, we need to detect if schema is a Zod or JSON Schema / AJV. And handle validation accordingly.
https://github.com/mercurius-js/mercurius/blob/master/lib/routes.js#L71
to fix it i do it like this @turkerdev :
fastify.setValidatorCompiler(({ schema }) => {
const ajv = new Ajv({
removeAdditional: 'all',
useDefaults: true,
coerceTypes: 'array',
// any other options
// ...
});
//@ts-ignore
if (schema.safeParse) {
return (data) => {
try {
//@ts-ignore
schema.parse(data);
return { value: data };
} catch (e) {
const validationError = fromZodError(e as ZodError);
//@ts-ignore
return { error: [{ message: validationError }] };
}
};
}
return ajv.compile(schema);
});
AND
await fastify.register(require('@fastify/swagger'), {
mode: 'dynamic',
exposeRoute: true,
openapi: {
info: {
title: softName,
version: pkg?.version,
},
host: 'localhost:' + port,
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
},
transform: ({ schema, url }) => {
const newSchema = { ...schema };
return createJsonSchemaTransform({
skipList: [
'/documentation/',
'/documentation/initOAuth',
'/documentation/json',
'/documentation/uiConfig',
'/documentation/yaml',
'/documentation/*',
'/documentation/static/*',
'/graphql',
'/graphiql',
'/graphiql/main.js',
'/graphiql/sw.js',
'/graphiql/config.js',
],
})({ schema: newSchema, url });
},
});
Because this lib is not maintained anymore, I have created a fork who fix all these issues : https://github.com/qlaffont/fastify-type-provider-zod2
@qlaffont This library is supported. Could you please send your changes as a PR with a test, so that we could properly review it?
You have everything on the repo if you want the src/index.ts is the same as the current library you just need to do a diff. And i have include some customs tests to test if everything is working on test folder.
@qlaffont I'm sorry but I honestly don't have time or energy to pull everything by hand and create a PR. Happy to review the PR if it's there.
@kibertoad or @turkerdev any update ?
any update?
Hello, I'm currently trying to use your lib, but it doesn't work when I add these lines :
I have this error :
Regards Quentin