textual.validation
This module provides a number of classes for validating input.
See Validating Input for details.
Failure
dataclass
¶
Function
¶
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
¶
validate
¶
validate(value)
Validate that the supplied function returns True.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
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
¶
Bases: Number
Validator which ensures the value is an integer which falls within a range.
NotAnInteger
dataclass
¶
validate
¶
validate(value)
Ensure that value
is an integer, optionally within a range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The value to validate. |
required |
Returns:
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |
Length
¶
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
¶
validate
¶
validate(value)
Ensure that value falls within the maximum and minimum length constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The value to validate. |
required |
Returns:
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |
Number
¶
Bases: Validator
Validator that ensures the value is a number, with an optional range check.
maximum
instance-attribute
¶
maximum = maximum
The maximum value of the number, inclusive. If None
, the maximum is unbounded.
minimum
instance-attribute
¶
minimum = minimum
The minimum value of the number, inclusive. If None
, the minimum is unbounded.
NotANumber
dataclass
¶
Bases: Failure
Indicates a failure due to the value not being a valid number (decimal/integer, inc. scientific notation)
NotInRange
dataclass
¶
validate
¶
validate(value)
Ensure that value
is a valid number, optionally within a range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The value to validate. |
required |
Returns:
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |
Regex
¶
Bases: Validator
A validator that checks the value matches a regex (via re.fullmatch
).
NoResults
dataclass
¶
Bases: Failure
Indicates validation failed because the regex could not be found within the value string.
validate
¶
validate(value)
Ensure that the value matches the regex.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The value that should match the regex. |
required |
Returns:
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |
URL
¶
Bases: Validator
Validator that checks if a URL is valid (ensuring a scheme is present).
InvalidURL
dataclass
¶
validate
¶
validate(value)
Validates that value
is a valid URL (contains a scheme).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The value to validate. |
required |
Returns:
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |
ValidationResult
dataclass
¶
ValidationResult(failures=list())
The result of calling a Validator.validate
method.
failure_descriptions
property
¶
failures
class-attribute
instance-attribute
¶
A list of reasons why the value was invalid. Empty if valid=True
failure
staticmethod
¶
failure(failures)
Construct a failure ValidationResult.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Sequence[Failure]
|
The failures. |
required |
Returns:
Type | Description |
---|---|
ValidationResult
|
A failure ValidationResult. |
success
staticmethod
¶
Construct a successful ValidationResult.
Returns:
Type | Description |
---|---|
ValidationResult
|
A successful ValidationResult. |
Validator
¶
Bases: ABC
Base class for the validation of string values.
Commonly used in conjunction with the Input
widget, which accepts a
list of validators via its constructor. This validation framework can also be used to validate any 'stringly-typed'
values (for example raw command line input from sys.args
).
To implement your own Validator
, subclass this class.
Example
failure_description
instance-attribute
¶
failure_description = failure_description
A description of why the validation failed.
The description (intended to be user-facing) to attached to the Failure if the validation fails.
This failure description is ultimately accessible at the time of validation failure via the Input.Changed
or Input.Submitted
event, and you can access it on your message handler (a method called, for example,
on_input_changed
or a method decorated with @on(Input.Changed)
.
describe_failure
¶
describe_failure(failure)
Return a string description of the Failure.
Used to provide a more fine-grained description of the failure. A Validator could fail for multiple reasons, so this method could be used to provide a different reason for different types of failure.
Warning
This method is only called if no other description has been supplied. If you supply a description
inside a call to self.failure(description="...")
, or pass a description into the constructor of
the validator, those will take priority, and this method won't be called.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Failure
|
Information about why the validation failed. |
required |
Returns:
Type | Description |
---|---|
str | None
|
A string description of the failure. |
failure
¶
failure(description=None, value=None, failures=None)
Shorthand for signaling validation failure.
You can return failure(...) from a Validator.validate
implementation to signal validation succeeded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str | None
|
The failure description that will be used. When used in conjunction with the Input widget,
this is the description that will ultimately be available inside the handler for |
None
|
|
str | None
|
The value that was considered invalid. This is optional, and only needs to be supplied if required
in your |
None
|
|
Failure | Sequence[Failure] | None
|
The reasons the validator failed. If not supplied, a generic |
None
|
Returns:
Type | Description |
---|---|
ValidationResult
|
A ValidationResult representing failed validation, and containing the metadata supplied to this function. |
success
¶
Shorthand for ValidationResult(True)
.
You can return success() from a Validator.validate
method implementation to signal
that validation has succeeded.
Returns:
Type | Description |
---|---|
ValidationResult
|
A ValidationResult indicating validation succeeded. |
validate
abstractmethod
¶
validate(value)
Validate the value and return a ValidationResult describing the outcome of the validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The value to validate. |
required |
Returns:
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |