Skip to content

API ​

makeValidator(schema, options?) ​

Compiles a schema into a validator. Compiling is the slow part, so make each validator once and reuse it.

ParameterTypeDefaultDescription
schemaSchema<T> | JsonSchema<T>—A Juniper schema, or a rendered JSON Schema object. Juniper schemas are rendered with $schema.
optionsOmit<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 strict can'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's keywords option. The exception is Ajv's strictTuples check, off by default because it rejects open tuples (arraySchema().prefixItem(...)); pass strictTuples: true to turn it on.
  • discriminator is on. Ajv's discriminator option defaults to true, for Juniper's discriminated unions. Pass discriminator: false to turn it off.
  • JSON Pointer paths. jsPropertySyntax can't be passed, so each issue's instancePath is a JSON Pointer, which its path is read from.
  • Options that change the value. coerceTypes, useDefaults and removeAdditional change the value passed in, and validate() returns that same value.
  • Standard formats. email, uuid, date-time and the other formats of ajv-formats are validated. Add your own with Ajv's formats option:
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>.

MemberTypeDescription
is(value: unknown) => value is TReturns whether the value matches, narrowing it to T.
assert(value: unknown) => asserts value is TThrows 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.
~standardStandardJSONSchemaV1.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 ​

TypeExampleDescription
ValidatorTypeValidatorType<typeof validator>The type a validator checks for. The same as SchemaType of its schema.
AssertionTypeAssertionType<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
>;
type Amount = number
const
assertAmount
:
AssertionType
<typeof
validator
> =
validator
.
assert
;