Skip to content

Juniper Arbitrary ​

npm · Source · Changelog

Generate test values from a Juniper schema, typed by it and always valid against it.

ts
import * as 
fc
from 'fast-check';
import {
numberSchema
,
objectSchema
,
stringSchema
} from 'juniper';
import {
makeArbitrary
} from 'juniper-arbitrary';
const
user
=
objectSchema
({
properties
: {
email
:
stringSchema
().
format
('email'),
age
:
numberSchema
().
type
('integer').
minimum
(0),
},
required
: ['email'],
});
fc
.
assert
(
fc
.
property
(
makeArbitrary
(
user
),
value
=> {
console
.
log
(
value
);
value: {
    email: string;
    age?: number;
}
}) );

Get started →

Features ​

  • Valid by construction, then checked: Generators aim at each keyword, and a validator has the last word, so a value that comes out always validates.
  • Typed by the schema: An Arbitrary of the schema's SchemaType, with no type to write by hand.
  • Fails loudly: A schema nothing matches throws with the path to the part responsible, where a plain .filter() would hang.
  • Plain JSON: Values survive a JSON round trip unchanged, with no -0, undefined or null-prototype objects.

Packages

  • Juniper

    Build JSON Schemas in TypeScript with inferred static types — strict, Ajv-ready, JSON Schema 2020-12 and OpenAPI 3.0 output.

  • Juniper Validator

    Standard Schema compliant validators for Juniper schemas, with type guards and assertions.

Used in demos

  • OpenAPI

    A notes API from spec to database: an OpenAPI 3.1 document built from Juniper schemas, genever types for a typed client and a Hono server that validates every request and response, and Drizzle on Postgres with a database per test.