validation.rules

Page actions AI-ready formats and sharing
Open LLM text
Share with AI
Ask Claude Ask ChatGPT Ask Gemini Ask Copilot

Built-in validation rules for chirp forms.

Each validator is a callable with the signature::

def rule(value: str) -> str | None:
    '''Return error message, or None if valid.'''

Parameterized validators are factory…

Built-in validation rules for chirp forms.

Each validator is a callable with the signature::

def rule(value: str) -> str | None:
    '''Return error message, or None if valid.'''

Parameterized validators are factory functions that return a validator::

def max_length(n: int) -> Callable[[str], str | None]:
    def check(value: str) -> str | None:
        if len(value) > n:
            return f"Must be at most {n} characters"
        return None
    return check

Custom validators follow the same protocol — any callable matching (str) -> str | None works with validate().

validation.rules

Name Type Default Description
type
qualified_name
element_type
description
source_file
line_number
is_autodoc
autodoc_element
_autodoc_template
_autodoc_url_path
_autodoc_page_type
title
doc_content_hash

Symbols on this page

required
function
def required(value: str) -> str | None

Field must be present and non-empty.

Parameters

Name Type Default Description
value str
max_length
function
def max_length(n: int) -> Validator

String must be at most n characters.

Parameters

Name Type Default Description
n int
min_length
function
def min_length(n: int) -> Validator

String must be at least n characters.

Parameters

Name Type Default Description
n int
email
function
def email(value: str) -> str | None

Value must be a valid email address (basic format check).

Parameters

Name Type Default Description
value str
url
function
def url(value: str) -> str | None

Value must be a valid URL (http/https).

Parameters

Name Type Default Description
value str
matches
function
def matches(pattern: str, message: str | None = None) -> Validator

Value must match the given regex pattern.

Parameters

Name Type Default Description
pattern str
message str | None None
one_of
function
def one_of(*choices: str) -> Validator

Value must be one of the given choices.

Parameters

Name Type Default Description
*choices str
integer
function
def integer(value: str) -> str | None

Value must be a valid integer.

Parameters

Name Type Default Description
value str
number
function
def number(value: str) -> str | None

Value must be a valid number (int or float).

Parameters

Name Type Default Description
value str

View source · /home/runner/work/chirp/chirp/site/../src/chirp/validation/rules.py:1