Validation

A fluent, expressive validation engine. Sanitize inputs, enforce complex rules, and ensure data integrity with a type-safe Python API.

Rules Engine

Chain multiple constraints like required, email, and min:8 using a simple syntax.

Sanitization

Built-in helpers to clean strings, escape HTML, and normalize data formats (like Dates and Booleans) automatically.

Type Safe

Includes a Rule class helper to provide IntelliSense and prevent string typos in your validation logic.

1

Direct Validation

Use static methods for quick, single-value checks. These methods return the sanitized value on success, or None on failure.

Python
from casp.validate import Validate

email = Validate.email("  User@Example.com ")
# Result: "User@Example.com" (Trimmed & Validated)

is_valid = Validate.cuid2("tz4a98xxat96iws9zmbrgj3a")
# Result: "tz4a98xxat96iws9zmbrgj3a"

invalid = Validate.url("not-a-url")
# Result: None
2

The Rules Engine

For complex logic, use with_rules. You can pass a pipe-separated string (Laravel style) or use the Rule class for better DX.

src/app/auth/actions.py
from casp.validate import Validate, Rule

def register_user(data):
    username = data.get("username")
    
    # 1. String Syntax (Quick & Dirty)
    check = Validate.with_rules(username, "required|alpha_num|min:3|max:20")
    
    # 2. Class Syntax (Recommended for Type Safety)
    check_safe = Validate.with_rules(username, [
        Rule.REQUIRED,
        Rule.min(3),
        Rule.max(20),
        Rule.not_in_list(["admin", "root"])
    ])

    if check_safe is not True:
        return {"error": check_safe} # Returns specific error message
    
    return {"success": True}

Available Rules

Rule Name Example Usage Description
required Rule.REQUIRED Checks if value is not null, empty string, or empty list.
email Rule.EMAIL Validates email format (supports email-validator if installed).
min / max Rule.min(5) Enforces string length constraints.
confirmed Rule.confirmed() Checks if value matches a confirmation_value passed to with_rules.
in / notIn Rule.in_list(["A", "B"]) Ensures the value exists (or does not exist) in a provided list.
regex Rule.regex(r"^[a-z]+$") Validates against a custom Regular Expression.