Validators#
The oblate.validate module provides some built-in commonly used validators and also provides the
utilities to define your own validators.
Field Validator#
- oblate.validate.field(field, *, raw=False)#
A decorator to register a validator for a field.
The decorated function takes three parameters, the schema (self), the value being validated and the
oblate.LoadContextinstance.- Parameters:
field (Union[
oblate.fields.Field,str]) – The field or name of field that the validator is for.raw (
bool) – Whether the validator is a raw validator.
Base class#
- class oblate.validate.Validator#
The base class for validators.
This class offers an alternative interface to the more commonly used
oblate.validate.field()decorator. Validators provided by Oblate inherit from this base class.Subclasses are required to override the
validate()method. When subclassing,rawsubclass parameter can be set to True to mark the validator as a raw validator.Tip
This class is a
typing.Genericand takes a single type argument, the type of value which will be validated.- validate(value, context, /)#
Validates a value.
This is an abstract method that must be implemented by the subclasses.
If the validation fails, either one of
AssertionError,ValueErrororoblate.FieldErrorshould be raised.- Parameters:
value – The value to validate.
context (
LoadContext) – The deserialization context.
Prebuilt Validators#
- class oblate.validate.Range(lb=..., ub=..., /)#
Validates the range of a
Integerfield.Initialization of this validator is similar to Python’s
range()function with an exception that upper bound is inclusive. That is:Range(5)must be between 0-5 inclusive (equivalent toRange(0, 5))Range(2, 10)must be between 2-10 inclusive
New in version 1.1.
- Parameters:
lb (
int) – The lower bound. This parameter acts as an upper bound if noubis provided and lower bound is defaulted to 0.ub (
int) – The upper bound. If not provided,lbis considered upper bound and 0 as lower bound.
- classmethod from_standard(obj, /)#
Constructs a
validate.Rangefrom the Python’srange()object.When constructed with this function, the returned range validator instance would only include the integers accounted by the given standard
range()(i.e upper bound not inclusive).
- class oblate.validate.Length(*, min=..., max=..., exact=...)#
Validates the length of a sized structure.
This validator could be applied on any structure compatible with the
collections.abc.Sizedprotocol.Initialization examples:
Length(min=10): minimum length 10 charactersLength(max=10): maximum length 10 charactersLength(min=10, max=20): length between 10 to 20 charactersLength(exact=10): length exactly 10 characters
New in version 1.1.
- Parameters:
min (
int) – The minimum length.max (
int) – The maximum length.exact (
int) – The exact length. Cannot be mixed with other two parameters.
- class oblate.validate.Regex(pattern, flags=0, fail_message='Value failed pattern validation', full_match=False, search=False)#
Validates a value using the given regular expression.
By default, this uses
re.Pattern.match()to match the pattern in given value. Usefull_matchorsearchparameters to change this behaviour.New in version 1.1.
- Parameters:
pattern (Union[
str,re.Pattern]) – The pattern to validate from.flags (
int) – Thereflags to pass to pattern. Only applicable whenpatternis a string.fail_message (
str) –The error message if the given value doesn’t match the pattern. This can be formatted by using following values:
{value}: The value being validated{pattern}: The pattern used for validation
full_match (
bool) – If true, usesre.Pattern.fullmatch()on the string. This checks that string is exact as the pattern. Defaults to False.search (
bool) – If true, usesre.Pattern.search()on the string. This simply checks if the pattern is in the string at any location. Defaults to False.
- class oblate.validate.Exclude(*values)#
A validator that specifies the values that cannot be passed to a field.
New in version 1.1.
- Parameters:
*values – The values to exclude.
- class oblate.validate.Or(*validators)#
A validator that passes if any one of the given validators pass.
The validators being passed can either be
validate.Validatorsubclasses or function based validators. In both cases, if any of the validator pass, this validator passes.New in version 1.1.
- Parameters:
*validators – The validators to run.