Validators from an API contract
Generate truefit validators and their TypeScript types from an OpenAPI or AsyncAPI contract with contractdiff's validator target, or read the contract's schemas at run time with fromJsonSchema.
When the source of truth is an OpenAPI or AsyncAPI document, write the validators from it rather than by hand. Two ways:
Ahead of time, with contractdiff
contractdiff's validator target reads the contract and
writes truefit validators and their types, one per schema, as source you commit. The output is
emitted code, so it runs with no truefit at run time and no eval, and regenerating it on every
contract change keeps the validators and the contract in step. See contractdiff's docs for the
command and its options.
At run time, with fromJsonSchema
import { issues } from 'truefit';
import { jsonSchemaReader } from 'truefit/json-schema';
const components = {
components: {
schemas: {
Pet: { type: 'object', required: ['id', 'name'], properties: { id: { type: 'integer' }, name: { type: 'string' }, tag: { type: 'string', nullable: true } } },
},
},
};
const reader = jsonSchemaReader(components, { draft: 'openapi-3.0' });
const Pet = reader.read({ $ref: '#/components/schemas/Pet' });
console.log(reader.findings);
console.log(issues(Pet, { id: 1, name: 'Rex', tag: null }), issues(Pet, { id: '1' }).map((i) => i.path));[]
[] [ '/id', '/name' ]openapi-3.0 reads the Schema Object's own rules: nullable adds null, exclusiveMinimum is a
boolean, and a $ref silences its siblings.
Under a strict Content-Security-Policy
Run validation in a browser extension, an Electron renderer or a page whose Content-Security-Policy forbids unsafe-eval: truefit's check needs no eval, and truefit/emit gives you compiled guards as source at build time.
truefit
Every export of truefit, with its signature and doc comment: parse, VENDOR, string, number, boolean, literal and 112 more, plus 59 types.