# Validate Data

> Validates the input payload against a JSON Schema (Draft-07).

Source: https://docs.sommatic.ai/en/nodes/data.validate/

- Slug: `data.validate`
- Family: Data
- Version: 1.0.0
- Status: Available
- Execution: sync

Validates the input payload against a JSON Schema (Draft-07).

Ensures data quality and integrity by strictly enforcing structure, types, and constraints before proceeding in the workflow. If validation fails, execution is routed to the 'invalid' port with detailed error metadata.

**Features:**
- Uses AJV (Another JSON Validator) under the hood.
- Supports full JSON Schema specification.
- Provides detailed error reports in the metadata.

**Use Cases:**
- Verifying API request bodies.
- Ensuring mandatory fields exist before database insertion.
- Validating configuration parameters.

## Configuration

- `schema` (object, required): The schema object to validate against.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `valid` (default): Data conforms to schema.
- `invalid`: Data violates matches schema.
- `error`: Schema compilation or system error.

## Input contract

- `payload` (object, optional)

## Output contract

- `payload` (object, optional)

## In expressions

What a later node can read from this node's output with `$("Validate Data").item.json.<path>` (write your node's label instead of `Validate Data`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `valid`, from the output shown in Ports.

- `$("Validate Data").item.json.payload` (object) **The worker does not return it: reads as undefined.**
- `$("Validate Data").item.json.id` (string) = `"pay_88"` (Not suggested by the editor)
- `$("Validate Data").item.json.amount` (number) = `120.5` (Not suggested by the editor)


## How it works

Validates the incoming payload against a JSON Schema with Ajv 8 and routes the unchanged payload to `valid` or `invalid`.

1. Resolves the config. `schema` can be an object or a JSON string (as the code editor saves it); a string is parsed.
2. Fails when the schema is missing or not an object.
3. Compiles the schema on every run with Ajv (`allErrors: true`, strict mode on, draft-07 by default).
4. Valid: the payload leaves through `valid`.
5. Invalid: the payload leaves through `invalid` and every Ajv error is listed in `meta.validation_errors`.

## In a workflow

### Node

```json
{
  "node_id": "node-validate-payment",
  "label": "Validate payment",
  "operator_slug": "data.validate",
  "operator_version": "1.0.0",
  "config": {
    "schema": {
      "type": "object",
      "required": [
        "id",
        "amount"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "amount": {
          "type": "number",
          "minimum": 0
        }
      }
    }
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "id": "pay_88",
    "amount": 120.5
  },
  "port": "valid"
}
```

## Output ports (behaviour in the code)

### `valid`

The payload matches the schema.

```json
{
  "id": "pay_88",
  "amount": 120.5
}
```

### `invalid`

The payload violates the schema. `data` is the unchanged payload.

```json
{
  "id": "pay_88",
  "amount": "120.5"
}
```

### `error`

The schema is missing, is not valid JSON or does not compile.

```json
{
  "ok": false,
  "errors": [
    "Validation Error: strict mode: unknown keyword: \"required_fields\""
  ],
  "port": "error"
}
```

## Behaviour in the code

- `schema`: String values inside the schema containing `{{ }}` are resolved by the Expression Engine before compiling.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Schema is not valid JSON: <message>` | `error` | `schema` is a string that is not valid JSON. |
| `Schema definition is required and must be an object` | `error` | `schema` is missing or resolved to a non-object. |
| `Validation Error: strict mode: unknown keyword: "<keyword>"` | `error` | The schema uses a keyword Ajv does not know. Strict mode rejects it at compile time. |
| `Validation Error: unknown format "<format>" ignored in schema at path "<path>"` | `error` | `format` (`email`, `date-time`, `uri`...) needs ajv-formats, which is not loaded, so any `format` fails compilation. Use `pattern` instead. |

## Notes and limits

- `meta` is recorded in the run steps only: downstream nodes receive `data` as their payload and cannot read `meta` through `$json` or `$("Node")`.
- `invalid` is not an error: the envelope has `ok: true`. Connect `invalid` to the branch that handles rejected data.
- When the envelope has no `data` (or `data` is falsy), the engine stores the whole envelope as the node output: on `error` the next node receives `{ ok: false, errors: [...], port: "error" }` as its payload.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "data.validate",
  "name": "Validate Data",
  "category": "data",
  "version": "1.0.0",
  "description": "Validates the input payload against a JSON Schema (Draft-07).\n\n  Ensures data quality and integrity by strictly enforcing structure, types, and constraints before proceeding in the workflow. If validation fails, execution is routed to the 'invalid' port with detailed error metadata.\n\n  **Features:**\n  - Uses AJV (Another JSON Validator) under the hood.\n  - Supports full JSON Schema specification.\n  - Provides detailed error reports in the metadata.\n\n  **Use Cases:**\n  - Verifying API request bodies.\n  - Ensuring mandatory fields exist before database insertion.\n  - Validating configuration parameters.",
  "capabilities": [
    "validation",
    "schema",
    "json",
    "quality"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "schema"
    ],
    "properties": {
      "schema": {
        "type": "object",
        "title": "JSON Schema",
        "description": "The schema object to validate against.",
        "ui_widget": "code_editor",
        "ui_options": {
          "language": "json"
        }
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "description": "The payload to validate.",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "description": "The original payload (if valid).",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "valid",
      "label": "Valid",
      "is_default": true,
      "description": "Data conforms to schema."
    },
    {
      "port_id": "invalid",
      "label": "Invalid",
      "description": "Data violates matches schema."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Schema compilation or system error."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "validation",
    "quality",
    "schema"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "schema"
  ],
  "properties": {
    "schema": {
      "type": "object",
      "title": "JSON Schema",
      "description": "The schema object to validate against.",
      "ui_widget": "code_editor",
      "ui_options": {
        "language": "json"
      }
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "description": "The payload to validate.",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "The original payload (if valid).",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "valid",
    "label": "Valid",
    "is_default": true,
    "description": "Data conforms to schema."
  },
  {
    "port_id": "invalid",
    "label": "Invalid",
    "description": "Data violates matches schema."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Schema compilation or system error."
  }
]
```
