Validators

1 minute read

Boltzmann provides three validator decorators that rely on the ajv schema validator for you to enforce a schema for your route parameters, query params, and body. The functions are:

Here's an example of a route parameter that must be a uuid:

identityDetail.route = 'GET /identities/identity/:id'
identityDetail.decorators = [
  boltzmann.decorators.params({
    type: 'object',
    required: ['id'],
    properties: {
      id: { type: 'string', format: 'uuid' }
    }
  })
]
async function identityDetail (/** @type {Context} */ context) { }

The boltzmann.decorators.params function takes an ajv schema and generates a decorator that applies the schema to any route params supplied.