Validators

A validator is a callable for model field that takes a value and raises a ValidationError if it doesn’t meet some criteria.

Usage

You can pass a list of validators to Field parameter validators:

class ValidatorModel(Model):
    regex = fields.CharField(max_length=100, null=True, validators=[RegexValidator("abc.+", re.I)])

# oh no, this will raise ValidationError!
await ValidatorModel.create(regex="ccc")
# this is great!
await ValidatorModel.create(regex="abcd")

Built-in Validators

Here is the list of built-in validators:

class tortoise.validators.CommaSeparatedIntegerListValidator(allow_negative=False)[source]

A validator to validate whether the given value is valid comma separated integer list or not.

class tortoise.validators.MaxLengthValidator(max_length)[source]

A validator to validate the length of given value whether greater than max_length or not.

class tortoise.validators.MaxValueValidator(max_value)[source]

Max value validator for FloatField, IntField, SmallIntField, BigIntField

class tortoise.validators.MinLengthValidator(min_length)[source]

A validator to validate the length of given value whether less than min_length or not.

class tortoise.validators.MinValueValidator(min_value)[source]

Min value validator for FloatField, IntField, SmallIntField, BigIntField

class tortoise.validators.RegexValidator(pattern, flags)[source]

A validator to validate the given value whether match regex or not.

class tortoise.validators.Validator[source]
abstract __call__(value)[source]

All specific validators should implement this method.

Parameters:
value

The given value to be validated.

Raises:

ValidationError – if validation failed.

tortoise.validators.validate_ipv46_address(value)[source]

A validator to validate whether the given value is valid IPv4Address or IPv6Address or not.

Raises:

ValidationError – if value is invalid IPv4Address or IPv6Address.

tortoise.validators.validate_ipv4_address(value)[source]

A validator to validate whether the given value is valid IPv4Address or not.

Raises:

ValidationError – if value is invalid IPv4Address.

tortoise.validators.validate_ipv6_address(value)[source]

A validator to validate whether the given value is valid IPv6Address or not.

Raises:

ValidationError – if value is invalid IPv6Address.

Custom Validator

There are two methods to write a custom validator, one you can write a function by passing a given value, another you can inherit tortoise.validators.Validator and implement __call__.

Here is a example to write a custom validator to validate the given value is an even number:

from tortoise.validators import Validator
from tortoise.exceptions import ValidationError

class EvenNumberValidator(Validator):
    """
    A validator to validate whether the given value is an even number or not.
    """
    def __call__(self, value: int):
        if value % 2 != 0:
            raise ValidationError(f"Value '{value}' is not an even number")

# or use function instead of class
def validate_even_number(value:int):
    if value % 2 != 0:
        raise ValidationError(f"Value '{value}' is not an even number")