Jsontron adds four keywords to a schema object. They can appear in any schema object, at the root or under properties, items, $defs and the applicators, and together they form the object's rule set.
| Keyword | Value | Role |
|---|---|---|
rules |
a non-empty array of rules | each rule selects context nodes and tests them |
assert |
an item, or a non-empty array of items | tests that must hold for the instance |
report |
the same forms as assert |
tests that must not hold |
let |
a non-empty object of names to jq expressions | variables for the three above |
The syntax is published as the meta-schema jsontron-0.2.json on SchemaStore, and every implementation checks it when the schema is built.
rulesA rule has a context, and at least one of asserts and reports, each a non-empty array of items. It may also have an id (see labels) and a let.
{
"rules": [
{
"id": "promotions",
"context": ".lineItems[] | select(.sku | startswith(\"PROMO-\"))",
"asserts": [
{
"test": ".qty <= ($root.promotions[.sku].maxQty // 1)",
"message": "promo \\(.sku) allows at most \\($root.promotions[.sku].maxQty // 1), got \\(.qty)"
}
],
"reports": [
{ "test": ".price < 0", "message": "promo \\(.sku) has a negative price" }
]
}
]
}
context is evaluated with . set to the instance, the value the schema object applies to. Each value it produces is a context node, and every assert and report of the rule is tested on each one, in order. A context that produces no value checks nothing.
An item is an object with a test and, optionally, a message, an id, and a flag or a role. Without message, the text of a failure is the text of test itself; see Messages and labels.
assertassert is shorthand for a rule about the instance itself, a rule whose context is .:
{
"type": "object",
"properties": {
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0 }
},
"assert": ".age >= 18 or (.guardianEmail | type) == \"string\""
}
It takes three forms:
| Form | Meaning |
|---|---|
"assert": "<jq>" |
one item; its failure text is the expression |
"assert": { "test": "<jq>", "message": "..." } |
one item, with a message or labels |
"assert": [ ... ] |
several items, strings or objects |
{
"assert": [
".status | IN(\"draft\", \"submitted\", \"approved\")",
{
"test": ".status != \"approved\" or .approver != null",
"message": "an approved document needs an approver"
}
]
}
reportreport is the inverse of assert, as in Schematron: an item fails when its test is truthy. Use it to call out a state that must not happen rather than to require one that must.
{
"report": {
"test": ".password | test(\"password\"; \"i\")",
"message": "the password must not contain the word 'password'"
}
}
report takes the same three forms as assert. Inside a rule, reports are the reports array, beside asserts.
The four keywords of one schema object make one rule set, evaluated once:
rules, in order;assert and report are added to the first rule whose context is exactly "." (asserts after its asserts, reports after its reports), or to a new rule { "context": "." } at the end when there is none;let applies to the whole rule set.So rules, assert and report can be mixed freely on the same schema object.
letlet binds jq variables, so that a lookup or a derived value is computed once and reused. It is allowed on the schema object and on each rule:
{
"let": {
"cap": ".policy.maxLineItems",
"activeCustomers": "[.customers[] | select(.active) | .id]"
},
"rules": [
{
"context": ".orders[]",
"let": { "lines": ".lineItems | length" },
"asserts": [
{
"test": ".customerId | IN($activeCustomers[])",
"message": "customer \\(.customerId) is not active"
},
{
"test": "$lines <= $cap",
"message": "order \\(.id) has \\($lines) lines, over the cap of \\($cap)"
}
]
}
]
}
. set to the instance, and is visible in every expression of the rule set. A rule-level binding is evaluated for each context node, with . set to that node, after context; it is visible in that rule's tests and messages, not in its context, and hides a schema-level binding of the same name.null when it produces none.[A-Za-z_][A-Za-z0-9_]*. root is reserved: $root is always the document.A schema object with let but none of the other three keywords has no rule set: its bindings are checked when the schema is built, and never evaluated.
The keywords apply to the instance at their schema location. Under properties or items, . is the nested value, while $root is still the whole document:
{
"type": "object",
"properties": {
"shipments": {
"type": "array",
"items": {
"type": "object",
"required": [ "weightKg", "method" ],
"assert": {
"test": ".method != \"air\" or .weightKg <= $root.limits.maxAirKg",
"message": "air shipment \\(.id // \"?\") is over the air weight limit"
}
}
}
}
}
The errors of a nested rule set are reported at the location of the nested instance, here /shipments/0, /shipments/1, and so on.
For a large array, one rule on the array with a context that selects the items is often faster than a rule set under items that reads $root, since the document is then handed to jq once instead of once per item.