-
-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
342 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ Reference | |
exceptions | ||
signal | ||
migration | ||
validators |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
.. _validators: | ||
|
||
========== | ||
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`: | ||
|
||
.. code-block:: python3 | ||
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: | ||
|
||
.. automodule:: tortoise.validators | ||
:members: | ||
:undoc-members: | ||
|
||
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: | ||
|
||
.. code-block:: python3 | ||
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from tests.testmodels import ValidatorModel | ||
from tortoise.contrib import test | ||
from tortoise.exceptions import ValidationError | ||
|
||
|
||
class TestValues(test.TestCase): | ||
async def test_validator_regex(self): | ||
with self.assertRaises(ValidationError): | ||
await ValidatorModel.create(regex="ccc") | ||
await ValidatorModel.create(regex="abcd") | ||
|
||
async def test_validator_max_length(self): | ||
with self.assertRaises(ValidationError): | ||
await ValidatorModel.create(max_length="aaaaaa") | ||
await ValidatorModel.create(max_length="aaaaa") | ||
|
||
async def test_validator_ipv4(self): | ||
with self.assertRaises(ValidationError): | ||
await ValidatorModel.create(ipv4="aaaaaa") | ||
await ValidatorModel.create(ipv4="8.8.8.8") | ||
|
||
async def test_validator_ipv6(self): | ||
with self.assertRaises(ValidationError): | ||
await ValidatorModel.create(ipv6="aaaaaa") | ||
await ValidatorModel.create(ipv6="::") | ||
|
||
async def test_validator_comma_separated_integer_list(self): | ||
with self.assertRaises(ValidationError): | ||
await ValidatorModel.create(comma_separated_integer_list="aaaaaa") | ||
await ValidatorModel.create(comma_separated_integer_list="1,2,3") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.