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
schemamust be a dictionary of key-value mappings. Values must be callable validators. SeeXX.The
entireargument 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_keysargument must be one ofACCEPT,IGNOREorREJECT.The
required_errorargument specifies the error message used when a key is missing.REQUIRED_ERRORis 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
datadictionary and returns transformed values.Will raise
decent.error.Invalidif 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:
Noneis converted toFalse.intvalues areTrueexcept for0.strvalues converted in lower- and uppercase:y, yes, t, truen, 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
Nonewith 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
Invaliderror. 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.Numberonly, excluding booleans.The error messages raised can be customized with
min_messageandmax_message. Theminandmaxarguments 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.UUIDunlessto_uuidisFalse.
decent.error¶
-
exception
decent.error.DecentError¶ Bases:
Exception
-
exception
decent.error.Error(message, path=None)¶ Bases:
decent.error.DecentErrorA single validation error.
The
messagecontains an explanation for the error: for example, “this value must be at least 10 characters long”.The
pathis a list of keys to the field this error is for. This is usually automatically set by thedecent.schema.Schemaand/or validator callable being used.-
as_dict(join='.')¶ Returns the error as a path to message dictionary. Paths are joined with the
joinstring.
-
messages¶
-
paths¶
-
-
exception
decent.error.Invalid(errors=None)¶ Bases:
decent.error.ErrorA 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
joinstring.
-
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