Skip to content

ValidationRule

A single validation rule that can be applied to a string value.

Supports the built-in rule types 'required', 'email', 'stringLength', 'pattern', and 'custom', and carries a trigger policy that controls when the rule is evaluated.

Attributes:

Name Type Description
type RuleType

The validation rule type.

message str

Custom error message; if empty a default is generated.

trigger RuleTriggerType

When the rule fires — 'always', 'blur', or 'manual'.

params dict

Additional parameters specific to the rule type (e.g., min/max for 'stringLength', pattern for 'pattern', func for 'custom').

__init__

__init__(rule_type: RuleType, message: str = '', **kwargs)

Create a validation rule.

Parameters:

Name Type Description Default
rule_type RuleType

The type of validation to apply.

required
message str

Custom error message. If empty, a sensible default is used.

''
**kwargs

Rule-specific parameters. Pass trigger to override the default trigger policy; all other keys are stored in params (e.g., min=3, max=20 for 'stringLength', pattern=r'\d+' for 'pattern', func=callable for 'custom').

{}

validate

validate(value: str) -> ValidationResult

Apply this rule to a value and return the result.

Parameters:

Name Type Description Default
value str

The string value to validate.

required

Returns:

Type Description
ValidationResult

A ValidationResult with is_valid=True on success or is_valid=False

ValidationResult

with an error message on failure.