Skip to main content
The Schema API provides JSON Schema-based validation with automatic type inference for Feathers services.

schema

Creates a schema wrapper that provides validation and type inference from a JSON Schema definition.

Parameters

JSONSchemaDefinition
required
JSON Schema definition object with $id property
Ajv
default:"DEFAULT_AJV"
Custom AJV validator instance. Defaults to the built-in validator with coerceTypes: true

Returns

Returns a SchemaWrapper<S> instance with the following members:
<T>(data: T) => Promise<T>
Validates data against the schema. Returns validated (possibly coerced) data or throws BadRequest error
Record<string, JSONSchema>
The schema’s property definitions
readonly string[]
Array of required property names
JSONSchemaDefinition
The original schema definition
FromSchema<S>
Type inference helper (TypeScript only). Use with Infer<typeof schema> to extract the inferred type

Types

JSONSchemaDefinition

Extends standard JSON Schema with required $id property:

Validator

A validation function type:

Infer

Extract the TypeScript type from a schema:

Combine

Combine a schema type with additional properties:

SchemaWrapper

The class returned by schema() that wraps a JSON Schema definition.

validate

Validates and coerces data according to the schema:
any
required
Data to validate
Returns: Promise<T> - Validated and coerced data Throws: BadRequest - If validation fails

toJSON

Returns the underlying schema definition:

DEFAULT_AJV

The default AJV instance used by schemas:

addFormats

Add format validators to an AJV instance:

Parameters

Ajv
required
AJV instance to add formats to
FormatName[] | FormatsPluginOptions
Array of format names or configuration object

Example Usage

Basic Schema

Custom Validation

With Service