Valify
A JSON schema library.
Assertion
Valify includes a compiler to compile JSON schemas to assertion functions.
import { schema } from "@bit-js/valify";
import { evaluate, keywords } from "@bit-js/valify/compiler/assertion";
const User = schema({
type: "object",
required: ["name", "age"],
properties: {
name: {
type: "string",
minLength: 3,
maxLength: 32,
},
age: {
type: "integer",
exclusiveMinimum: 0,
exclusiveMaximum: 150,
},
},
});
const isUser = evaluate(User, keywords.draft6);
To compile the schema ahead-of-time:
import { schema } from "@bit-js/valify";
import { inspect, keywords } from "@bit-js/valify/compiler/assertion";
const User = schema({
type: "object",
required: ["name", "age"],
properties: {
name: {
type: "string",
minLength: 3,
maxLength: 32,
},
age: {
type: "integer",
exclusiveMinimum: 0,
exclusiveMaximum: 150,
},
},
});
const content = `export const isUser=(()=>{${inspect(User, keywords.draft6)}})();`;
Compilation options include:
evaluate(User, keywords.draft6, {
noNonFiniteNumber: true;
strictStringWidth: true;
strictPropertyCheck: true;
noArrayObject: true;
accurateMultipleOf: true;
unicodeAwareRegex: true;
fastAssertions: false;
});
Note
This library is experimental and still in active development.
Currently only draft 6 keywords (without format
, $ref
and $id
keywords) are supported.