Skip to content

validate

validate: (schemas?, options?) => (req, res, acc) => { response: { detail: string; details?: undefined; statusCode: number; }; } | { response: { detail: any; details: any; statusCode: any; }; } | undefined

Defined in: http/validate.js:132

Creates a JSON Schema validation middleware.

Accepts either a targeted form ({body?, query?, params?}) or a shorthand form (a raw JSON Schema object interpreted as body validation). The shorthand is detected when the first argument contains at least one JSON_SCHEMA_INDICATORS keyword and none of the VALID_SCHEMA_KEYS. Unrecognized keys that are neither targeted keys nor JSON Schema indicators emit a per-key-set ERGO_VALIDATE_UNKNOWN_KEY warning.

A schema map with body, query, and/or params keys (targeted form), or a raw JSON Schema object interpreted as body validation (shorthand form)

object

JSON Schema for the parsed request body

object

JSON Schema for route path parameters (reads acc.route.params or acc.params)

object

JSON Schema for parsed query parameters

AJV options forwarded to each compiled validator

object

Additional AJV constructor options

boolean

Report all errors instead of stopping at the first

boolean

Coerce input values to match schema types

boolean | object | string[]

Format keyword support via ajv-formats; forwarded to createValidator. Defaults to all standard formats enabled

(req, res, acc) => { response: { detail: string; details?: undefined; statusCode: number; }; } | { response: { detail: any; details: any; statusCode: any; }; } | undefined

// Shorthand form — raw JSON Schema interpreted as body validation
validate({
type: 'object',
properties: {name: {type: 'string'}},
required: ['name']
});
// Targeted form — explicit body, query, and/or params schemas
validate({
body: {type: 'object', properties: {name: {type: 'string'}}, required: ['name']},
query: {type: 'object', properties: {page: {type: 'string', pattern: '^[0-9]+$'}}}
});