Prerequisites
🚀 Feature Proposal
I suggest making 2 methods available (it doesn't matter for which object fastifyInstance or FastifyRequest)
vadidate(customSchema, data)
serialize(customSchema, data))
or simply expose/reexport Ajv and fast-json-stringify instances/fabrics to reuse them
Motivation
Sometimes it is necessary to make a request to a third-party service and I would like to be able to validate its parameters with the specified schema and then serialize the data into a string with the specified schema
Now in order to do this, I need to configure the project and include packages ajv and fast-json-stringify as a direct dependency and then make sure that they are the same version as in fastify in order not to have duplicates and not to increase the size of the application.
They need to be configured and created - this is additional code that duplicates the code in fastify.
Example
app.get('/getSomeComplexData', async (req, res) => {
const validationSchema = validationSchemas['Method1'];
const stringifySchema = serializationSchemas['Method1'];
const params = getParamsForMethod1(req);
if (req.validate(validationSchema, params) === false) {
throw new Error('Validation of parameters for the Method1 failed');
}
const method1Data = await fetch('http://some_service/method1').json();
const calcData = calculateSomedata();
return { result: {
calcData,
method1Result: req.serialize(stringifySchema , method1Data) // for some reason we need a string instead of json
}}
});
Prerequisites
🚀 Feature Proposal
I suggest making 2 methods available (it doesn't matter for which object fastifyInstance or FastifyRequest)
vadidate(customSchema, data)serialize(customSchema, data))or simply expose/reexport
Ajvandfast-json-stringifyinstances/fabrics to reuse themMotivation
Sometimes it is necessary to make a request to a third-party service and I would like to be able to validate its parameters with the specified schema and then serialize the data into a string with the specified schema
Now in order to do this, I need to configure the project and include packages
ajvandfast-json-stringifyas a direct dependency and then make sure that they are the same version as infastifyin order not to have duplicates and not to increase the size of the application.They need to be configured and created - this is additional code that duplicates the code in
fastify.Example