Validation
Framework for validating string values
Failure
class
¶
Function
class
¶
Bases: Validator
A flexible validator which allows you to provide custom validation logic.
function
instance-attribute
¶
Function which takes the value to validate and returns True if valid, and False otherwise.
ReturnedFalse
class
¶
Bases: Failure
Indicates validation failed because the supplied function returned False.
describe_failure
method
¶
validate
method
¶
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
class
¶
Bases: Number
Validator which ensures the value is an integer which falls within a range.
describe_failure
method
¶
validate
method
¶
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
class
¶
Bases: Validator
Validate that a string is within a range (inclusive).
maximum
instance-attribute
¶
The inclusive maximum length of the value, or None if unbounded.
minimum
instance-attribute
¶
The inclusive minimum length of the value, or None if unbounded.
Incorrect
class
¶
Bases: Failure
Indicates a failure due to the length of the value being outside the range.
describe_failure
method
¶
validate
method
¶
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
class
¶
Bases: Validator
Validator that ensures the value is a number, with an optional range check.
maximum
instance-attribute
¶
The maximum value of the number, inclusive. If None
, the maximum is unbounded.
minimum
instance-attribute
¶
The minimum value of the number, inclusive. If None
, the minimum is unbounded.
NotANumber
class
¶
Bases: Failure
Indicates a failure due to the value not being a valid number (decimal/integer, inc. scientific notation)
NotInRange
class
¶
Bases: Failure
Indicates a failure due to the number not being within the range [minimum, maximum].
describe_failure
method
¶
validate
method
¶
Ensure that value
is a valid number, 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. |
Regex
class
¶
Bases: Validator
A validator that checks the value matches a regex (via re.fullmatch
).
NoResults
class
¶
Bases: Failure
Indicates validation failed because the regex could not be found within the value string.
describe_failure
method
¶
validate
method
¶
Ensure that the value matches the regex.
Parameters
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
The value that should match the regex. |
required |
Returns
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |
URL
class
¶
Bases: Validator
Validator that checks if a URL is valid (ensuring a scheme is present).
describe_failure
method
¶
validate
method
¶
Validates that value
is a valid URL (contains a scheme).
Parameters
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
The value to validate. |
required |
Returns
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |
ValidationResult
class
¶
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
¶
Construct a failure ValidationResult.
Parameters
Name | Type | Description | Default |
---|---|---|---|
failures |
Sequence[Failure]
|
The failures. |
required |
Returns
Type | Description |
---|---|
ValidationResult
|
A failure ValidationResult. |
merge
staticmethod
¶
Merge multiple ValidationResult objects into one.
Parameters
Name | Type | Description | Default |
---|---|---|---|
results |
Sequence['ValidationResult']
|
List of ValidationResult objects to merge. |
required |
Returns
Type | Description |
---|---|
'ValidationResult'
|
Merged ValidationResult object. |
success
staticmethod
¶
Construct a successful ValidationResult.
Returns
Type | Description |
---|---|
ValidationResult
|
A successful ValidationResult. |
Validator
class
¶
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
¶
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
method
¶
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 |
Failure
|
Information about why the validation failed. |
required |
Returns
Type | Description |
---|---|
str | None
|
A string description of the failure. |
failure
method
¶
Shorthand for signaling validation failure.
You can return failure(...) from a Validator.validate
implementation to signal validation succeeded.
Parameters
Name | Type | Description | Default |
---|---|---|---|
description |
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
|
value |
str | None
|
The value that was considered invalid. This is optional, and only needs to be supplied if required
in your |
None
|
failures |
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
method
¶
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 the value and return a ValidationResult describing the outcome of the validation.
Parameters
Name | Type | Description | Default |
---|---|---|---|
value |
str
|
The value to validate. |
required |
Returns
Type | Description |
---|---|
ValidationResult
|
The result of the validation. |