We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
you can use Zod schema for example
this is a user schema
const schema = z.object({ firstName: z.string(), lastName: z.string().optional(), email: z.string().email().describe("unique"), username: z.string().min(3).max(20).describe("unique"), password: z.string() })
you can use preHandler on fastify to validate your schema like this
const requestValidation = (schema: ZodObject<any>) => { return async (req: FastifyRequest, reply: FastifyReply) => { if (!schema) return; const result = schema.safeParse(req.body); if (!req.body) { return reply.status(400).send({ success: false, message: 'Validation error', errors: [ { code: 'MISSING_BODY', path: 'root', message: 'Request body is missing' } ] }) } if (!result.success) { const errors = result.error.issues.map(issue => ({ code: issue.code, path: issue.path.length > 0 ? issue.path.join('.') : 'root', message: issue.message })) reply.status(400).send({ success: false, message: 'Validation error', errors }) } else { return } } } fastify.route({ method: "POST, url: "/users/create", preHandler: requestValidation(schema), handler: async (req, reply) => { const data = {} reply.send(data) } })
preHandler will validate your req before the handler
In that example, I’m using that schema as a Mongoose schema as well
The text was updated successfully, but these errors were encountered:
tode in #22
Sorry, something went wrong.
Michael-Liendo
Successfully merging a pull request may close this issue.
you can use Zod schema for example
this is a user schema
you can use preHandler on fastify to validate your schema like this
preHandler will validate your req before the handler
In that example, I’m using that schema as a Mongoose schema as well
The text was updated successfully, but these errors were encountered: