# Validator Nodes

## Overview

Validator nodes are essential components within Dynamiq's platform that perform data validation within your workflows. These nodes act as quality control checkpoints, ensuring that data meets specific criteria before proceeding through the workflow.

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2FBt7uo1P5q58LGx35aSTb%2Fimage.png?alt=media&#x26;token=82c276fc-0094-4b0b-85b0-81bceedd1505" alt=""><figcaption><p>Validator nodes in Dynamiq</p></figcaption></figure>

### Key Features

* Flexible validation rules
* Configurable error handling behavior
* Support for multiple data formats
* Built-in error messaging

### Available Validators

#### 1. Valid Python (ValidPython)

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2FPyJLXlbiHnKln3Ahijk4%2Fimage.png?alt=media&#x26;token=afcad773-2069-4d2a-8b63-56b8c646b575" alt=""><figcaption><p>Available validators on Dynamiq</p></figcaption></figure>

Validates that the input content follows the correct Python syntax.

**Example Usage:**

```python
{
    "content": "def hello_world():\n    print('Hello, World!')"
}
```

#### 2. Valid JSON (ValidJSON)

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2FA4tPBrZMGTV7aB8mTVbx%2Fimage.png?alt=media&#x26;token=10c53071-c903-4b4b-9649-3332d52a5170" alt=""><figcaption></figcaption></figure>

Ensures input content is properly formatted JSON. Handles both string and dictionary inputs.

**Example Usage:**

```python
{
    "content": {"name": "John", "age": 30}
}
```

#### 3. Valid Choices (ValidChoices)

Verifies that the input content matches one of the predefined acceptable values.

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2F5msxyt1ILCwqt0cA2RuO%2Fimage.png?alt=media&#x26;token=b3837403-f345-45eb-9652-d958283f69b4" alt=""><figcaption></figcaption></figure>

**Example Configuration:**

```python
{
    "choices": ["option1", "option2", "option3"],
    "content": "option1"
}
```

#### 4. Regex Match (RegexMatch)

Validates the input content against a specified regular expression pattern.

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2FsfZtWEGsFTwy1vV3qsMX%2Fimage.png?alt=media&#x26;token=d7709e9a-5637-4ee2-ab87-8ba18c02ef65" alt=""><figcaption></figcaption></figure>

**Match Types:**

* `fullmatch`: Entire string must match the pattern
* `search`: Pattern can match anywhere in the string

**Example Configuration:**

```python
{
    "regex": "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}$",
    "match_type": "fullmatch",
    "content": "user@example.com"
}
```

### Configuration Options

#### General Settings

Each validator node shares these common configuration options:

<table><thead><tr><th width="130">Setting</th><th>Description</th><th>Options</th></tr></thead><tbody><tr><td>Name</td><td>Node identifier in the workflow</td><td>Text input</td></tr><tr><td>Behavior</td><td>Action on validation failure</td><td><code>RAISE</code>, <code>RETURN</code></td></tr></tbody></table>

#### Validator-Specific Settings

**Valid Choices**

* **Choices**: List of acceptable values
* **Strip whitespace**: Automatically applied to string inputs

**Regex Match**

* **Regex**: Regular expression pattern
* **Match\_type**: `FULL_MATCH` or `SEARCH`

### Response Format

When `behavior` is set to `RETURN`, validators output:

```python
{
    "valid": bool,  # Validation result
    "content": Any  # Original input content
}
```

When `behavior` is set to `RAISE`, validators:

* Return the input content if validation passes
* Raise a `ValueError` with detailed error message if validation fails

### Best Practices

1. **Error Handling**
   * Use `RAISE` behavior for critical validation points
   * Use `RETURN` behavior when you need to handle invalid data gracefully
2. **Input Sanitization**
   * Consider using RegexMatch for input sanitization
   * Valid Choices for enforcing controlled vocabularies
3. **Performance**
   * Place validators early in workflows to fail fast
   * Use appropriate match types in RegexMatch to optimize performance

### Common Use Cases

1. **Form Validation**

```python
{
    "type": "RegexMatch",
    "regex": "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}$",
    "behavior": "RAISE"
}
```

2. **Configuration Validation**

```python
{
    "type": "ValidJSON",
    "behavior": "RETURN"
}
```

3. **Code Quality Checks**

```python
{
    "type": "ValidPython",
    "behavior": "RAISE"
}
```

### Troubleshooting

Common validation errors and solutions:

<table><thead><tr><th width="250">Error</th><th>Possible Cause</th><th>Solution</th></tr></thead><tbody><tr><td>"Value is not valid JSON"</td><td>Malformed JSON string</td><td>Check JSON syntax and structure</td></tr><tr><td>"Value is not in valid choices"</td><td>Input not in choices list</td><td>Verify input against allowed values</td></tr><tr><td>"Value does not match pattern"</td><td>Regex pattern mismatch</td><td>Test pattern with regex debugger</td></tr><tr><td>"Value is not valid python code"</td><td>Python syntax error</td><td>Check code formatting</td></tr></tbody></table>
