Skip to content

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#

  1. In assertions, select + add assertion and choose json_schema.
  2. The operator and value boxes change to the text the JSON body must match this schema and a large text box.
  3. 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.
Example schema
json
{
  "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.
  • $ref lets 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.
  • pattern uses the same regular expressions as matches (4.1).
  • Whole numbers larger than 2^53 are not compared exactly.

Limits#

LimitValue
Size of all schemas on one monitor256 KiB
Objects in one schema2,000
Levels of nesting in one schema64
JSON values in the response100,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 valuesA value is not in the schema's list.
response body is not JSONCheck the address. Add a status_code assertion.
response body has more than 100000 JSON values, too many to validateCheck a smaller response, or use JSON path (4.5).
validation did not finish within the check's timeoutMake the schema simpler, or raise the timeout (3.1).

Last updated