validate
Validates accumulator properties against JSON Schemas compiled at factory
time using AJV. Supports body, query parameter,
and route parameter schemas. Includes ajv-formats for standard format
keywords (email, uri, date-time, uuid, etc.) out of the box.
Pipeline stage: Validation (after body() and url())
Import
Section titled “Import”import { validate } from "@centralping/ergo";Options
Section titled “Options”The validate() factory accepts two arguments: a schemas object and an
options object — validate(schemas, options).
Schemas (first argument)
Section titled “Schemas (first argument)”| Property | Type | Default | Description |
|---|---|---|---|
body | object | — | JSON Schema for the parsed request body |
query | object | — | JSON Schema for query parameters |
params | object | — | JSON Schema for route path parameters |
These are direct properties of the first argument, not nested under a
wrapper key. For example: validate({body: schema}), not
validate({schemas: {body: schema}}).
Options (second argument)
Section titled “Options (second argument)”| Property | Type | Default | Description |
|---|---|---|---|
formats | boolean | string[] | object | all standard | ajv-formats configuration |
formats Values
Section titled “formats Values”| Value | Behavior |
|---|---|
undefined or true | All standard formats enabled |
false | Formats disabled |
['email', 'uri'] | Only listed formats enabled |
{ mode: 'fast' } | Full ajv-formats plugin config |
Route Parameter Resolution
Section titled “Route Parameter Resolution”Route parameters are resolved from acc.route?.params (ergo-router) with
a fallback to acc.params (standalone).
Return Value
Section titled “Return Value”The middleware returns undefined on success — validation is a gate, not
a data producer.
Error Responses
Section titled “Error Responses”| Status | Condition |
|---|---|
| 422 Unprocessable Entity | Validation failure — response includes details array with per-field errors |
| 500 Internal Server Error | A body schema is configured but acc.body is missing — body() middleware was not placed before validate() in the pipeline. Emits a one-time ERGO_VALIDATE_NO_BODY process warning |
import { compose, body, url, validate } from "@centralping/ergo";
const pipeline = compose( {fn: body(), setPath: "body"}, {fn: url(), setPath: "url"}, validate({ body: { type: "object", properties: { name: { type: "string" } }, required: ["name"], }, query: { type: "object", properties: { page: { type: "string", pattern: "^[0-9]+$" }, }, }, }),);router.post("/users", { validate: { body: { type: "object", properties: { name: { type: "string", format: "email" } }, required: ["name"], }, params: { type: "object", properties: { id: { type: "string", minLength: 1 } }, required: ["id"], }, }, execute: (req, res, acc) => ({ response: { statusCode: 201, body: { created: true } }, }),});API Reference
Section titled “API Reference”See the auto-generated validate API docs.