Validators¶
Validators are ordinary callables that take a single argument (the input value) and return a transformed result value. They can also raise validation errors.
-
IValidator
(value)¶ Validates the input
value
and possibly transforms it.Returns the new value. If the value is invalid, raises a
decent.error.Error
ordecent.error.Invalid
exception.
Validator callables can be used inside schemas, but also standalone.
Built-in validators¶
Decent ships with many built-in validators that can be used and combined for usual tasks. Instances of them are built through the following constructors:
Helpers & Building blocks¶
-
decent.validators.
All
(*validators) Combines all the given validator callables into one, running all the validators in sequence on the given value.
-
decent.validators.
Any
(*validators) Combines all the given validator callables into one, running the given value through them in sequence until a valid result is given.
-
decent.validators.
Default
(default) Creates a validator callable that replaces
None
with the specified default value.
-
decent.validators.
Maybe
(validator) Wraps the given validator callable, only using it for the given value if it is not
None
.
-
decent.validators.
Msg
(validator, message) Wraps the given validator callable, replacing any error messages raised.
Basics¶
-
decent.validators.
Eq
(value, message='Not equal to {!s}') Creates a validator that compares the equality of the given value to
value
.A custom message can be specified with
message
. It will be formatted withvalue
.
-
decent.validators.
Coerce
(type, message='Not a valid {} value') Creates a validator that attempts to coerce the given value to the specified
type
. Will raise an error if the coercion fails.A custom message can be specified with
message
.
-
decent.validators.
Instance
(expected, message='Not an instance of {}') Creates a validator that checks if the given value is an instance of
expected
.A custom message can be specified with
message
.
-
decent.validators.
Type
(expected, message='Not of type {}') Creates a validator that compares the type of the given value to
expected
. This is a direct type() equality check. Also seeInstance
, which is an isinstance() check.A custom message can be specified with
message
.
Collections¶
-
decent.validators.
List
(validator) Creates a validator that runs the given validator on every item in a list or other collection. The validator can mutate the values.
Any raised errors will be collected into a single
Invalid
error. Their paths will be replaced with the index of the item. Will raise an error if the input value is not iterable.
-
decent.validators.
Length
(min=None, max=None, min_message='Must have a length of at least {min}', max_message='Must have a length of at most {max}') Creates a validator that checks if the given value’s length is in the specified range, inclusive. (Returns the original value.)
See
Range()
.
Booleans¶
-
decent.validators.
Boolean
() Creates a validator that attempts to convert the given value to a boolean or raises an error. The following rules are used:
None
is converted toFalse
.int
values areTrue
except for0
.str
values converted in lower- and uppercase:y, yes, t, true
n, no, f, false
Numbers¶
-
decent.validators.
Range
(min=None, max=None, min_message='Must be at least {min}', max_message='Must be at most {max}') Creates a validator that checks if the given numeric value is in the specified range, inclusive.
Accepts values specified by
numbers.Number
only, excluding booleans.The error messages raised can be customized with
min_message
andmax_message
. Themin
andmax
arguments are formatted.
-
decent.validators.
Length
(min=None, max=None, min_message='Must have a length of at least {min}', max_message='Must have a length of at most {max}') Creates a validator that checks if the given value’s length is in the specified range, inclusive. (Returns the original value.)
See
Range()
.
Strings¶
-
decent.validators.
Lower
() Creates a validator that converts the input string to lowercase. Will raise an error for non-string types.
-
decent.validators.
Upper
() Creates a validator that converts the input string to UPPERCASE. Will raise an error for non-string types.
-
decent.validators.
Strip
() Creates a validator that strips the input string of whitespace. Will raise an error for non-string types.
-
decent.validators.
Length
(min=None, max=None, min_message='Must have a length of at least {min}', max_message='Must have a length of at most {max}') Creates a validator that checks if the given value’s length is in the specified range, inclusive. (Returns the original value.)
See
Range()
.
String conversions¶
-
decent.validators.
Uuid
(to_uuid=True) Creates a UUID validator. Will raise an error for non-string types and non-UUID values.
The given value will be converted to an instance of
uuid.UUID
unlessto_uuid
isFalse
.