> ## Documentation Index
> Fetch the complete documentation index at: https://snowglobe.so/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Validation

## Validator

```python theme={null}
@dataclass
class Validator()
```

Base class for validators.

#### \_*init*\_

```python theme={null}
def __init__(on_fail: Optional[Union[Callable[[Any, FailResult], Any],
                                     OnFailAction]] = None,
             **kwargs)
```

#### validate

```python theme={null}
def validate(value: Any, metadata: Dict[str, Any]) -> ValidationResult
```

Do not override this function, instead implement \_validate().

External facing validate function. This function acts as a wrapper for \_validate() and is intended to apply any meta- validation requirements, logic, or pre/post processing.

#### validate\_stream

```python theme={null}
def validate_stream(chunk: Any,
                    metadata: Dict[str, Any],
                    *,
                    property_path: Optional[str] = "$",
                    context_vars: Optional[ContextVar[Dict[
                        str, ContextVar[List[str]]]]] = None,
                    context: Optional[Context] = None,
                    **kwargs) -> Optional[ValidationResult]
```

Validates a chunk emitted by an LLM. If the LLM chunk is smaller than the validator's chunking strategy, it will be accumulated until it reaches the desired size. In the meantime, the validator will return None.

If the LLM chunk is larger than the validator's chunking strategy, it will split it into validator-sized chunks and validate each one, returning an array of validation results.

Otherwise, the validator will validate the chunk and return the result.

#### with\_metadata

```python theme={null}
def with_metadata(metadata: Dict[str, Any])
```

Assigns metadata to this validator to use during validation.

#### to\_runnable

```python theme={null}
def to_runnable() -> Runnable
```

#### register\_validator

```python theme={null}
def register_validator(
    name: str,
    data_type: Union[str, List[str]],
    has_guardrails_endpoint: bool = False
) -> Callable[[Union[Type[V], Callable]], Union[Type[V], Type[Validator]]]
```

Register a validator for a data type.

## ValidationResult

```python theme={null}
class ValidationResult(IValidationResult, ArbitraryModel)
```

ValidationResult is the output type of Validator.validate and the abstract base class for all validation results.

**Attributes**:

* `outcome` *str* - The outcome of the validation. Must be one of "pass" or "fail".
* `metadata` *Optional\[Dict\[str, Any]]* - The metadata associated with this validation result.
* `validated_chunk` *Optional\[Any]* - The value argument passed to validator.validate or validator.validate\_stream.

## PassResult

```python theme={null}
class PassResult(ValidationResult, IPassResult)
```

PassResult is the output type of Validator.validate when validation succeeds.

**Attributes**:

* `outcome` *Literal\["pass"]* - The outcome of the validation. Must be "pass".
* `value_override` *Optional\[Any]* - The value to use as an override if validation passes.

## FailResult

```python theme={null}
class FailResult(ValidationResult, IFailResult)
```

FailResult is the output type of Validator.validate when validation fails.

**Attributes**:

* `outcome` *Literal\["fail"]* - The outcome of the validation. Must be "fail".
* `error_message` *str* - The error message indicating why validation failed.
* `fix_value` *Optional\[Any]* - The auto-fix value that would be applied if the Validator's on\_fail method is "fix".
* `error_spans` *Optional\[List\[ErrorSpan]]* - Segments that caused validation to fail.

## ErrorSpan

```python theme={null}
class ErrorSpan(IErrorSpan, ArbitraryModel)
```

ErrorSpan provide additional context for why a validation failed. They specify the start and end index of the segment that caused the failure, which can be useful when validating large chunks of text or validating while streaming with different chunking methods.

**Attributes**:

* `start` *int* - Starting index relative to the validated chunk.
* `end` *int* - Ending index relative to the validated chunk.
* `reason` *str* - Reason validation failed for this chunk.

## ValidatorLogs

```python theme={null}
class ValidatorLogs(IValidatorLog, ArbitraryModel)
```

Logs for a single validator execution.

**Attributes**:

* `validator_name` *str* - The class name of the validator
* `registered_name` *str* - The snake\_cased id of the validator
* `property_path` *str* - The JSON path to the property being validated
* `value_before_validation` *Any* - The value before validation
* `value_after_validation` *Optional\[Any]* - The value after validation; could be different if `value_override`s or `fix`es are applied
* `validation_result` *Optional\[ValidationResult]* - The result of the validation
* `start_time` *Optional\[datetime]* - The time the validation started
* `end_time` *Optional\[datetime]* - The time the validation ended
* `instance_id` *Optional\[int]* - The unique id of this instance of the validator

## ValidatorReference

```python theme={null}
class ValidatorReference(IValidatorReference)
```

ValidatorReference is a serialized reference for constructing a Validator.

**Attributes**:

* `id` *Optional\[str]* - The unique identifier for this Validator. Often the hub id; e.g. guardrails/regex\_match.  Default None.
* `on` *Optional\[str]* - A reference to the property this validator should be applied against.  Can be a valid JSON path or a meta-property such as `prompt` or `output`. Default None.
* `on_fail` *Optional\[str]* - The OnFailAction to apply during validation. Default None.
* `args` *Optional\[List\[Any]]* - Positional arguments. Default None.
* `kwargs` *Optional\[Dict\[str, Any]]* - Keyword arguments. Default None.
