# JSON Schema

> How data.validate checks a payload against a JSON Schema (draft-07) — the ports it opens, what the validator accepts, and the strict rules that make a schema fail before it validates anything.

Source: https://docs.sommatic.ai/en/expressions/json-schema/ (updated 2026-11-26)

[`data.validate`](https://docs.sommatic.ai/en/nodes/data.validate.md) checks its input against a JSON Schema and routes the run by the result.
Put it right after a trigger or an extraction to stop bad data before it reaches a system of record.

## Ports

| Port      | When                                                        |
| --------- | ----------------------------------------------------------- |
| `valid`   | The input matches the schema.                               |
| `invalid` | The input does not match. The run continues on this branch. |
| `error`   | The schema itself could not be read or compiled.            |

On `valid` and `invalid` the input passes through unchanged. The validation errors are recorded with the node's
execution, so you see them in the run. Connect `invalid` to the path that handles bad data: a reply to the sender, a
[task](https://docs.sommatic.ai/en/human-in-the-loop.md) for a person, or a log.

## Write the schema

The `schema` field takes a JSON Schema object. The validator is Ajv 8 with **draft-07**:

```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["body"],
  "properties": {
    "body": {
      "type": "object",
      "required": ["email", "amount", "status"],
      "properties": {
        "email": { "type": "string", "pattern": "^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$" },
        "amount": { "type": "number", "minimum": 0 },
        "status": { "enum": ["open", "closed"] },
        "items": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "object", "required": ["sku"] }
        }
      },
      "additionalProperties": false
    }
  }
}
```

Every draft-07 keyword except `format` works (see below): `type`, `required`, `properties`, `enum`, `const`, `pattern`, `minimum`,
`minLength`, `items`, `additionalProperties`, `if` / `then` / `else`, `allOf`, `anyOf`, `oneOf` and the rest. All
errors are reported, not only the first one.

## Strict rules

The validator runs in strict mode, so some schemas fail to compile and the node leaves through `error`:

| Fails                                   | Write instead                                                                     |
| --------------------------------------- | --------------------------------------------------------------------------------- |
| `"format": "email"` (or `date`, `uri`…) | A `pattern`, such as the email pattern above, or `^\d{4}-\d{2}-\d{2}$` for a date |
| Unknown keywords, such as `"example"`   | `"examples": [ … ]`, `"title"` and `"description"` are allowed                    |
| `"$schema"` of draft 2019-09 or 2020-12 | The draft-07 URI, or no `$schema`                                                 |

> **Note: No coercion, no defaults**
>
> The validator does not convert types or fill in defaults: the text `"3"` is not an `integer`, and `default` values
> are not added to the payload. Convert or complete the data before, with a
> [`data.transform`](https://docs.sommatic.ai/en/nodes/data.transform.md).

## Other places that use JSON Schema

Apps declare their input and output with JSON Schema contracts; see [contracts](https://docs.sommatic.ai/en/apps/contracts.md).

## Next steps

- [data.validate](https://docs.sommatic.ai/en/nodes/data.validate.md): Configuration and ports of the validation node.
- [Errors](https://docs.sommatic.ai/en/workflows/errors.md): Error ports and recovery paths.
