Skip to content

Make available the data validation method with the specified schema and the data serialization method with the specified schema #3897

@budarin

Description

@budarin

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 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
   }}
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestNew feature to be addedsemver-minorIssue or PR that should land as semver minor

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions