Skip to content

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 { validate } from "@centralping/ergo";

The validate() factory accepts two arguments: a schemas object and an options object — validate(schemas, options).

PropertyTypeDefaultDescription
bodyobjectJSON Schema for the parsed request body
queryobjectJSON Schema for query parameters
paramsobjectJSON 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}}).

PropertyTypeDefaultDescription
formatsboolean | string[] | objectall standardajv-formats configuration
ValueBehavior
undefined or trueAll standard formats enabled
falseFormats disabled
['email', 'uri']Only listed formats enabled
{ mode: 'fast' }Full ajv-formats plugin config

Route parameters are resolved from acc.route?.params (ergo-router) with a fallback to acc.params (standalone).

The middleware returns undefined on success — validation is a gate, not a data producer.

StatusCondition
422 Unprocessable EntityValidation failure — response includes details array with per-field errors
500 Internal Server ErrorA 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]+$" },
},
},
}),
);

See the auto-generated validate API docs.