What I want to do is add validation to the schema response from a fastify route.
Following the documentation from Fastify here we can see this
Ajv for the validation of a request fast-json-stringify for the serialization of a response's body
Related to improve and add validations for a response, what I want to do is check the schema when I send a response.
fast-json-stringify support different options, included format, but if you read the documentation, they said that they support JSON schema. Jsonschema has support for email format, that you can see here as a built-in format but when I try to use it on Fastify, like this:
{
response: {
200: {
type: 'object',
required: ['email'],
properties: {
email: {
type: 'string',
format: 'email',
}
}
}
}
}
And try to return ad response this
reply.code(200).send({ email: 'test' })
The only validation that I can do is when I set the type to integer and try to return a string.
Did you know if it's possible to use ajv-formats with fast-json-stringify to add validation to the response schema and use the formats from ajv and add new ones?
Many thanks in advance!
fast-json-stringify
does the serialization, not the validation.
The json schema provided to it will be used to serialize only the properties
declared and some type checking like integer
or array
s.
enum
keyword is not supportedformat
keyword is supported only for the dates as documented:To reach your goal, you should use this plugin: fastify-response-validation
that will add a validation step of your response body before the serialization process.