Skip to content

Output validation #961

@jsumners

Description

@jsumners

Currently, route configurations sort of overload the schema configuration. It doubles as input validation and for output serialization. This is confusing to users expecting it to be solely for validation of all potential targets. Thus, when combined with the ability to supply alternate validators, e.g. Joi, we encounter a situation where the user will write an output validation that causes errors. As an example:

server.route({
  path: '/foo',
  method: 'GET',
  schema: {
    response: {
      200: joi.object({ foo: joi.string().required() })
    }
  },
  handler (req, res) {
    res.send({foo: 'bar'})
  }
})

A user expecting schemas to be for validation may write this sort of schema and it will result in a circular reference error since Fastify is attempting to serialize according to the circular Joi schema.

The short solution is to replace the serializer for this route. However, that is not feasible when every route in a project should have the serializer replaced. Thus, we need to be able to supply a global serializer.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions