API
makeValidator(schema, options?)
Compiles a schema into a validator. Compiling is the slow part, so make each validator once and reuse it.
| Parameter | Type | Default | Description |
|---|---|---|---|
schema | Schema<T> | JsonSchema<T> | — | A Juniper schema, or a rendered JSON Schema object. Juniper schemas are rendered with $schema. |
options | Omit<AjvOptions, 'jsPropertySyntax' | 'strict'> | {} | Ajv options. |
Returns JuniperValidator<T>, typed by the schema.
Validation uses Ajv's JSON Schema 2020-12 validator (Ajv2020), which matches Juniper's output.
- Always strict. Strict mode is always on, and
strictcan't be passed. Juniper's output always passes it: keywords added with.metadata()are registered as annotations. A rendered JSON Schema with an unknown keyword is rejected; register it with Ajv'skeywordsoption. The exception is Ajv'sstrictTuplescheck, off by default because it rejects open tuples (arraySchema().prefixItem(...)); passstrictTuples: trueto turn it on. discriminatoris on. Ajv'sdiscriminatoroption defaults totrue, for Juniper's discriminated unions. Passdiscriminator: falseto turn it off.- JSON Pointer paths.
jsPropertySyntaxcan't be passed, so each issue'sinstancePathis a JSON Pointer, which itspathis read from. - Options that change the value.
coerceTypes,useDefaultsandremoveAdditionalchange the value passed in, andvalidate()returns that same value. - Standard formats.
email,uuid,date-timeand the other formats of ajv-formats are validated. Add your own with Ajv'sformatsoption:
ts
import { stringSchema } from 'juniper';
import { makeValidator } from 'juniper-validator';
const skuValidator = makeValidator(stringSchema().format('sku'), {
formats: { sku: /^[A-Z]{3}-\d{4}$/u },
});JuniperValidator<T>
Returned by makeValidator, and implements StandardSchemaV1<T> and StandardJSONSchemaV1<T>.
| Member | Type | Description |
|---|---|---|
is | (value: unknown) => value is T | Returns whether the value matches, narrowing it to T. |
assert | (value: unknown) => asserts value is T | Throws when the value doesn't match, and narrows it to T after the call. |
validate | (value: unknown) => StandardSchemaV1.Result<T> | Returns { value } when the value matches, or { issues } when it doesn't. |
~standard | StandardJSONSchemaV1.Props<T> & StandardSchemaV1.Props<T> | The Standard Schema and Standard JSON Schema interfaces. |
assert throws an Error with the message Value does not match schema, and Ajv's error objects as its cause. To narrow with it, its type must be written out. See assert().
Utility types
| Type | Example | Description |
|---|---|---|
ValidatorType | ValidatorType<typeof validator> | The type a validator checks for. The same as SchemaType of its schema. |
AssertionType | AssertionType<typeof validator> | The type of a validator's assert, to annotate it with. |
ts
import { numberSchema } from 'juniper';
import { makeValidator, type AssertionType, type ValidatorType } from 'juniper-validator';
const validator = makeValidator(numberSchema().minimum(0));
type Amount = ValidatorType<typeof validator>;
const assertAmount: AssertionType<typeof validator> = validator.assert;