Jsontron for Ajv: Schematron-style rules, assert,
report and let keywords for JSON Schema (draft 2020-12), whose context and test
expressions are jq, evaluated in-process by
@devlooped/jqlite (pure TypeScript, no
WebAssembly or native dependencies).
npm install ajv @devlooped/jsontron
import Ajv2020 from "ajv/dist/2020.js";
import { addJsontron } from "@devlooped/jsontron";
const ajv = addJsontron(new Ajv2020({ allErrors: true }));
const validate = ajv.compile({
type: "object",
required: ["orders", "policy", "approvedCustomers"],
rules: [
{
context: ".orders[] | select(.total > 1000)",
asserts: [
{
test: ".discount >= ($root.policy.minHighValueDiscount // 0.1)",
message: "High-value order \\(.id) discount \\(.discount) is below policy",
},
{
test: ".customer.id | IN($root.approvedCustomers[])",
message: "Customer \\(.customer.id) is not on the approved list",
},
],
},
],
});
const valid = validate({
policy: { minHighValueDiscount: 0.15 },
approvedCustomers: ["acme", "globex"],
orders: [
{ id: "O-1001", total: 2500, discount: 0.05, customer: { id: "initech" } },
{ id: "O-1002", total: 80, discount: 0, customer: { id: "unknown" } },
],
});
// O-1001 fails both asserts (discount + customer).
// O-1002 is ignored by the rule (total ≤ 1000).
console.log(valid); // false
console.log(validate.errors.map((e) => e.message));
// [ 'High-value order O-1001 discount 0.05 is below policy',
// 'Customer initech is not on the approved list' ]
The keywords, messages, labels and let bindings are described in the
root readme and defined by the specification.
addJsontron(ajv) adds the four keywords (rules, assert, report, let) to any Ajv
instance and returns it.ajv.compile validates every Jsontron keyword value and parses every jq expression in
isolation, throwing a JsontronSchemaError for invalid keyword shapes or invalid jq.jqlite expression
(reference lowering) and is reused on every
validation. The root document is passed in only when an expression references $root.Each failed assert or report is one Ajv error, at the instance location of the schema object that holds the keywords:
{ keyword: "rules", instancePath: "/order", schemaPath: "#/properties/order/rules",
message: "[orders][flag=warning] order O-1 discount is below policy", params: {} }
keyword is the keyword that evaluates the schema object's rule set: rules when present,
else assert, else report. A jq error while evaluating (in a let, context or test)
discards the failures of that rule set and raises a single error whose params is
{ evaluationError: "<jq error text>" }.
Each evaluation of a schema object converts its input to jqlite values (the instance, and
the whole document when an expression reads $root) and runs the compiled expression
in-process. For large arrays, prefer one rule at the level of the array with a context
expression over a rule set under items that reads $root, so the document is converted
once rather than per item.
ajv/dist/2020.specVersion); the meta-schema is exported as
metaSchema.npm install
npm test # unit tests and the conformance suite in ../tests
npm run typecheck
npm run build # dist/: ESM and type declarations