Skip to content

textual.validation

This module provides a number of classes for validating input.

See Validating Input for details.

Failure dataclass

Failure(validator, value=None, description=None)

Information about a validation failure.

description class-attribute instance-attribute

description = None

An optional override for describing this failure. Takes precedence over any messages set in the Validator.

validator instance-attribute

validator

The Validator which produced the failure.

value class-attribute instance-attribute

value = None

The value which resulted in validation failing.

Function

Function(function, failure_description=None)

Bases: Validator

A flexible validator which allows you to provide custom validation logic.

function instance-attribute

function = function

Function which takes the value to validate and returns True if valid, and False otherwise.

ReturnedFalse dataclass

ReturnedFalse(validator, value=None, description=None)

Bases: Failure

Indicates validation failed because the supplied function returned False.

describe_failure

describe_failure(failure)

Describes why the validator failed.

Parameters:

Name Type Description Default

failure

Failure

Information about why the validation failed.

required

Returns:

Type Description
str | None

A string description of the failure.

validate

validate(value)

Validate that the supplied function returns True.

Parameters:

Name Type Description Default

value

str

The value to pass into the supplied function.

required

Returns:

Type Description
ValidationResult

A ValidationResult indicating success if the function returned True, and failure if the function return False.

Integer

Integer(
    minimum=None, maximum=None, failure_description=None
)

Bases: Number

Validator which ensures the value is an integer which falls within a range.

NotAnInteger dataclass

NotAnInteger(validator, value=None, description=None)

Bases: Failure

Indicates a failure due to the value not being a valid integer.

describe_failure

describe_failure(failure)

Describes why the validator failed.

Parameters:

Name Type Description Default

failure

Failure

Information about why the validation failed.

required

Returns:

Type Description
str | None

A string description of the failure.

validate

validate(value)

Ensure that value is an integer, optionally within a range.

Parameters:

Name Type Description Default

value

str

The value to validate.

required

Returns:

Type Description
ValidationResult

The result of the validation.

Length

Length(
    minimum=None, maximum=None, failure_description=None
)

Bases: Validator

Validate that a string is within a range (inclusive).

maximum instance-attribute

maximum = maximum

The inclusive maximum length of the value, or None if unbounded.

minimum instance-attribute

minimum = minimum

The inclusive minimum length of the value, or None if unbounded.

Incorrect dataclass

Incorrect(validator, value=None, description=None)

Bases: Failure

Indicates a failure due to the length of the value being outside the range.

describe_failure

describe_failure(failure)

Describes why the validator failed.

Parameters:

Name Type Description Default

failure

Failure

Information about why the validation failed.

required

Returns:

Type Description
str | None

A string description of the failure.

validate

validate(value)

Ensure that value falls within the maximum and minimum length constraints.

Parameters:

Name Type Description Default

value

str

The value to validate.

required

Returns:

Type Description
ValidationResult

The result of the validation.

Number

Number(
    minimum=None, maximum=None, failure_description=None
)

Bases: