API listing¶
All submodule contents are available on the top-level decent
module.
decent.schema¶
-
class
decent.schema.
Schema
(schema, entire=None, extra_keys='IGNORE', required_error=None)¶ A schema that validates data given to it using the specified rules.
The
schema
must be a dictionary of key-value mappings. Values must be callable validators. SeeXX
.The
entire
argument allows specifying a callable validator that runs on the entire input after every field is validated. If provided, the validator will always run, even if validation errors are raised beforehand. Failed keys will not be included in the given data.The
extra_keys
argument must be one ofACCEPT
,IGNORE
orREJECT
.The
required_error
argument specifies the error message used when a key is missing.REQUIRED_ERROR
is the default.-
ACCEPT
= 'ACCEPT'¶
-
IGNORE
= 'IGNORE'¶
-
REJECT
= 'REJECT'¶
-
REJECT_ERROR
= 'This field is unknown.'¶
-
REQUIRED_ERROR
= 'This field is required.'¶
-
__call__
(data)¶ Validates the given
data
dictionary and returns transformed values.Will raise
decent.error.Invalid
if any validation errors are encountered.
-
-
class
decent.schema.
Marker
(key, default=None)¶ A base class for key markers that wrap a key.
-
class
decent.schema.
Default
(key, default=None)¶ A marker for specifying a default value for a key.
-
class
decent.schema.
Optional
(key, default=None)¶ A marker for specifying a key as optional. The schema will validate data without the key present.
decent.validators¶
-
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.
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
-
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.
Default
(default)¶ Creates a validator callable that replaces
None
with the specified default value.
-
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.
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.
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()
.
-
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.
Lower
()¶ Creates a validator that converts the input string to lowercase. Will raise an error for non-string types.
-
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.
-
decent.validators.
NotEmpty
()¶ Creates a validator that validates the given string is not empty. Will raise an error for non-string types.
-
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.
Strip
()¶ Creates a validator that strips the input string of whitespace. Will raise an error for non-string types.
-
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
.
-
decent.validators.
Upper
()¶ Creates a validator that converts the input string to UPPERCASE. Will raise an error for non-string types.
-
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
.
decent.error¶
-
exception
decent.error.
DecentError
¶ Bases:
Exception
-
exception
decent.error.
Error
(message, path=None)¶ Bases:
decent.error.DecentError
A single validation error.
The
message
contains an explanation for the error: for example, “this value must be at least 10 characters long”.The
path
is a list of keys to the field this error is for. This is usually automatically set by thedecent.schema.Schema
and/or validator callable being used.-
as_dict
(join='.')¶ Returns the error as a path to message dictionary. Paths are joined with the
join
string.
-
messages
¶
-
paths
¶
-
-
exception
decent.error.
Invalid
(errors=None)¶ Bases:
decent.error.Error
A collection of one or more validation errors for a schema.
-
append
(error)¶
-
as_dict
(join='.')¶ Returns all the errors in this collection as a path to message dictionary. Paths are joined with the
join
string.
-
message
¶ The first error message in this collection.
-
messages
¶ The list of error messages in this collection.
-
path
¶ The first error path in this collection.
-
paths
¶ The list of error paths in this collection.
-
-
exception
decent.error.
SchemaError
¶ Bases:
decent.error.DecentError