Validators¶
A validator takes a value and raise ValidationError
if it doesn`t meet some criteria.
Built-in validators¶
-
class
edipy.validators.
Range
(min_value, max_value)¶ Validate if a value is within a specific range of values
- Parameters
min_value – minimum value of the validation range.
max_value – maximum value of the validation range.
-
class
edipy.validators.
MaxValue
(max_value)¶ Validate if a value is greater than the limit
- Parameters
max_value – maximum value allowed.
-
class
edipy.validators.
MinValue
(min_value)¶ Validate if a value is less than the limit
- Parameters
min_value – minimum value allowed.
-
class
edipy.validators.
Regex
(pattern)¶ Validates if a value matches the pattern
- Parameters
pattern – regular expression
-
class
edipy.validators.
Email
¶ Validates if value is a valid email
How to extend¶
from edipy import validators, fields, exceptions, parser
class MyValidator(validators.Validator):
def validate(self, value):
if value != "edi":
raise exceptions.ValidationError(message=u"Value should be edi")
return True
class ValidatorExample(fields.EDIModel):
data = fields.String(3, validators=[MyValidator()])
try:
data = 'aaa'
example = parser.parse(ValidatorExample, data)
except exceptions.ValidationError as e:
print(e.message)