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).
- Structural validation stays with JSON Schema; cross-field business rules sit next to it.
- Implements the Jsontron 0.2 specification and passes its conformance suite.
- Fully synchronous in Node.js 20+ and browsers; Ajv strict mode stays on.
npm install ajv @devlooped/jsontronimport 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.compilevalidates every Jsontron keyword value and parses every jq expression in isolation, throwing aJsontronSchemaErrorfor invalid keyword shapes or invalid jq.- Each schema object with a rule set lowers to one compiled
jqliteexpression (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.
- Node.js 20 or later (ESM), or modern browsers.
- Ajv 8 (peer dependency); the suite runs with
ajv/dist/2020. - Implements Jsontron specification 0.2 (
specVersion); the meta-schema is exported asmetaSchema.
npm install
npm test # unit tests and the conformance suite in ../tests
npm run typecheck
npm run build # dist/: ESM and type declarations