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.
Parameters
Section titled “Parameters”schemas?
Section titled “schemas?”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
params?
Section titled “params?”object
JSON Schema for route path parameters (reads acc.route.params or acc.params)
query?
Section titled “query?”object
JSON Schema for parsed query parameters
options?
Section titled “options?”AJV options forwarded to each compiled validator
object
Additional AJV constructor options
allErrors?
Section titled “allErrors?”boolean
Report all errors instead of stopping at the first
coerceTypes?
Section titled “coerceTypes?”boolean
Coerce input values to match schema types
formats?
Section titled “formats?”boolean | object | string[]
Format keyword support via
ajv-formats; forwarded to createValidator. Defaults to all standard formats enabled
Returns
Section titled “Returns”(req, res, acc) => { response: { detail: string; details?: undefined; statusCode: number; }; } | { response: { detail: any; details: any; statusCode: any; }; } | undefined
Examples
Section titled “Examples”// Shorthand form — raw JSON Schema interpreted as body validationvalidate({ type: 'object', properties: {name: {type: 'string'}}, required: ['name']});// Targeted form — explicit body, query, and/or params schemasvalidate({ body: {type: 'object', properties: {name: {type: 'string'}}, required: ['name']}, query: {type: 'object', properties: {page: {type: 'string', pattern: '^[0-9]+$'}}}});