4.6JSON Schema
Type: json_schema. Checks: the whole JSON response against a JSON Schema. A JSON Schema is a standard way to describe JSON data: which fields it has, what type each one is, and which are required. It catches a renamed field, a number that became text, or a missing field. It works on URL, API and multi-step API monitors only. On other types it is refused: json_schema assertions apply to URL and API monitors only.
Operator: none. The value is the schema itself, as JSON text.
In the dashboard#
- In assertions, select + add assertion and choose
json_schema. - The operator and value boxes change to the text the JSON body must match this schema and a large text box.
- Paste your schema and save.
- If the text is not JSON, the form shows Not valid JSON: with the reason.
- The hint under the box reads: A $ref must point inside the schema. The schema is checked when you save.
- On the monitor page, the assertion reads
json_schema body matches schema. Select it to see the schema.
{
"type": "object",
"required": ["id", "items"],
"properties": {
"id": {"type": "integer"},
"items": {
"type": "array",
"items": {
"type": "object",
"required": ["sku", "price"],
"properties": {
"sku": {"type": "string"},
"price": {"type": "number"}
}
}
}
}
}What you can use#
- JSON Schema has versions, called drafts. obsrvly supports drafts 4, 6, 7, 2019-09 and 2020-12. Name your draft in
$schema. Without it, obsrvly uses 2020-12. format(for example, a date or an email address) is checked in draft 7 and earlier only.$reflets one part of a schema reuse another part. It must point inside the same schema, such as#/$defs/item. obsrvly does not fetch other files.patternuses the same regular expressions asmatches(4.1).- Whole numbers larger than 2^53 are not compared exactly.
Limits#
| Limit | Value |
|---|---|
| Size of all schemas on one monitor | 256 KiB |
| Objects in one schema | 2,000 |
| Levels of nesting in one schema | 64 |
| JSON values in the response | 100,000 |
obsrvly checks the schema when you save. If it cannot use it, saving fails with the reason. Examples: schema is not valid JSON, schema must be a JSON object, schema has more than 2000 objects, or schema refers to "...", which is outside the document; only references inside the schema are allowed.
When it fails: the message lists up to three problems, each with its place in the response. More problems end with ; and N more. For example, assertion json_schema: $.items[0].price: expected number, got string means the first item's price is text, not a number.
| Message (after "assertion json_schema:") | What to do |
|---|---|
missing required "id" | The response lacks a required field. |
unexpected "x" | The response has a field the schema does not allow. |
is not one of the allowed values | A value is not in the schema's list. |
response body is not JSON | Check the address. Add a status_code assertion. |
response body has more than 100000 JSON values, too many to validate | Check a smaller response, or use JSON path (4.5). |
validation did not finish within the check's timeout | Make the schema simpler, or raise the timeout (3.1). |