Form validation — composable rules, clean results.
Usage::
from chirp.validation import validate, required, max_length, email
async def create_post(request: Request):
form = await request.form()
result = validate(form, {
"title": [required, max_length(200)],
"body": [required],
"email": [required, email],
})
if not result:
return Template("form.html", form=form, errors=result.errors)
# result.data has cleaned values
validation
| 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
validate
function
def validate(data: Mapping[str, str] | dict[str, str], rules: dict[str, list[Validator]]) -> ValidationResult
Validate data against a set of rules.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
data
|
Mapping[str, str] | dict[str, str]
|
— | Any mapping of field names to string values — ``FormData``, ``QueryParams``, or a plain ``dict``. |
rules
|
dict[str, list[Validator]]
|
— | A dict mapping field names to lists of validator functions. Each validator returns an error message string on failure, or ``None`` on success. |
View source · /home/runner/work/chirp/chirp/site/../src/chirp/validation/__init__.py:1