Principle:Guardrails ai Guardrails Validator Logic Implementation
| Knowledge Sources | |
|---|---|
| Domains | Validation, Design_Pattern |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
A validation logic principle that defines the interface contract for implementing custom validation checks that return structured pass or fail results.
Description
Validator Logic Implementation defines how custom validation logic is structured in Guardrails. Every validator must implement a _validate method that receives the value to check and runtime metadata, then returns either a PassResult (optionally with a value override) or a FailResult (with an error message, optional fix value, and optional error spans marking the specific location of the issue).
This principle enforces a binary outcome model where every validation produces a clear pass or fail signal. The FailResult can optionally carry a fix_value that the framework uses when the on-fail action is FIX or FIX_REASK, enabling automatic correction without LLM re-asking.
Usage
Implement _validate in every custom Validator subclass. Return PassResult() for valid values, FailResult(error_message=..., fix_value=...) for invalid values. Use ErrorSpan to mark specific character ranges where errors occur.
Theoretical Basis
The validation interface contract:
# Abstract interface
def _validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult:
# Must return one of:
# PassResult() - Value is valid
# PassResult(value_override=x) - Value is valid, but use x instead
# FailResult(error_message="...") - Value is invalid
# FailResult(error_message="...", - Value is invalid, but can be fixed
# fix_value=corrected)
# FailResult(error_message="...", - Value is invalid at specific locations
# error_spans=[ErrorSpan(start, end, reason)])
...