Implementation:Guardrails ai Guardrails Validator Validate
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Validation, Design_Pattern |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
Concrete abstract method interface for implementing validation logic provided by the guardrails package.
Description
The Validator._validate method is the abstract method that all custom validators must override. It receives the value to validate (typed according to the validator's data_type) and a metadata dict passed from the Guard call. It must return a PassResult or FailResult. This is a Pattern Doc as it defines the interface users must implement, not a pre-built API.
Usage
Override this method in your Validator subclass. The method is called by the validation service during Guard execution.
Code Reference
Source Location
- Repository: guardrails
- File: guardrails/validator_base.py
- Lines: L176-183
Interface Specification
def _validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult:
"""User implementable function.
Validates a value and return a validation result. This method
should call _inference() in the implementation to perform
inference on some input value.
"""
raise NotImplementedError
Supporting Types
from guardrails.classes.validation.validation_result import (
PassResult, # Validation passed
FailResult, # Validation failed
ErrorSpan, # Mark error locations within text
)
# PassResult fields:
# value_override: Optional[Any] = None - Replace the value
# FailResult fields:
# error_message: str - Human-readable error
# fix_value: Optional[Any] = None - Automatic fix (used with FIX on_fail)
# error_spans: Optional[List[ErrorSpan]] = None - Error locations
# ErrorSpan fields:
# start: int - Start character index
# end: int - End character index
# reason: str - Why this span is an error
Import
from guardrails.validator_base import Validator
from guardrails.classes.validation.validation_result import (
PassResult, FailResult, ErrorSpan
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| value | Any | Yes | The value to validate (string, dict, list, etc. based on data_type) |
| metadata | Dict[str, Any] | Yes | Runtime metadata from Guard call |
Outputs
| Name | Type | Description |
|---|---|---|
| result | ValidationResult | PassResult for valid values, FailResult for invalid values |
Usage Examples
Simple String Validator
from guardrails.validator_base import Validator, register_validator
from guardrails.classes.validation.validation_result import PassResult, FailResult
@register_validator(name="my_org/no_profanity", data_type="string")
class NoProfanity(Validator):
BANNED_WORDS = {"badword1", "badword2"}
def _validate(self, value: str, metadata: dict):
words = set(value.lower().split())
found = words & self.BANNED_WORDS
if not found:
return PassResult()
cleaned = value
for word in found:
cleaned = cleaned.replace(word, "***")
return FailResult(
error_message=f"Found banned words: {found}",
fix_value=cleaned,
)
With Error Spans
from guardrails.classes.validation.validation_result import ErrorSpan
@register_validator(name="my_org/no_urls", data_type="string")
class NoURLs(Validator):
def _validate(self, value: str, metadata: dict):
import re
urls = list(re.finditer(r'https?://\S+', value))
if not urls:
return PassResult()
spans = [
ErrorSpan(start=m.start(), end=m.end(), reason="URL found")
for m in urls
]
return FailResult(
error_message=f"Found {len(urls)} URLs",
error_spans=spans,
)
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment