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.LoadContext instance.

Parameters:

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, raw subclass parameter can be set to True to mark the validator as a raw validator.

Tip

This class is a typing.Generic and 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, ValueError or oblate.FieldError should 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 Integer field.

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 to Range(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 no ub is provided and lower bound is defaulted to 0.

  • ub (int) – The upper bound. If not provided, lb is considered upper bound and 0 as lower bound.

classmethod from_standard(obj, /)#

Constructs a validate.Range from the Python’s range() 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.Sized protocol.

Initialization examples:

  • Length(min=10): minimum length 10 characters

  • Length(max=10): maximum length 10 characters

  • Length(min=10, max=20): length between 10 to 20 characters

  • Length(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. Use full_match or search parameters to change this behaviour.

New in version 1.1.

Parameters:
  • pattern (Union[str, re.Pattern]) – The pattern to validate from.

  • flags (int) – The re flags to pass to pattern. Only applicable when pattern is 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, uses re.Pattern.fullmatch() on the string. This checks that string is exact as the pattern. Defaults to False.

  • search (bool) – If true, uses re.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.Validator subclasses 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.