Skip to content

Validators

Aurélien edited this page Dec 3, 2017 · 1 revision

Validators are used to allow the fields to validate the data they load.
You can add validators to a field by using the validators argument when instantiating a field.

Builtin

DateTimeFormat

Equal

In

Length

Match

Type

Custom validator

If you need a custom validator it's easy.

  • your validator must be callable.
  • it must accept two arguments :
    • value the value to validate
    • path the path of the value, it's used for errors to know where the invalid value was in a document
  • it raises a subclass of lupin.errors.ValidationError if the value is invalid

Example, validation of odd numbers :

from lupin.errors import ValidationError

class NotOdd(ValidationError):
    pass

def is_odd(value, path):
    if value % 2 == 0:
        raise NotOdd("%s is not odd" % value, path)
Clone this wiki locally