validators module¶
-
class
campos.validators.
Validator
(message='Invalid data')[source]¶ Bases:
object
Base class for field validation.
Subclasses must define a
__call__(self, field)
method which validates the current value of field and raises a ValueError error if it is not valid.Parameters: message ( str
) – message to show when an invalid value is found.
-
class
campos.validators.
DataRequired
(message='Required')[source]¶ Bases:
campos.validators.Validator
Checks if a field is not empty.
Only basic validations are done here, the following values are considered invalid:
- None
- Empty strings or containing only blank spaces.
- Numbers when
math.isfinite()
is False. - Empty collections.
- Time objects if its evaluation in a boolean context is False
Parameters: message ( str
) – message to show when no data is found
-
class
campos.validators.
NumberRange
(min=0, max=100, message=None)[source]¶ Bases:
campos.validators.Validator
Number range validation.
Parameters: - min (
int
orfloat
) – minimum valid value, defaults to 0 - max (
int
orfloat
) – maximum valid value, defaults to 100 - message (
str
) – message to show ifnot min <= value <= max
- min (
-
class
campos.validators.
StringLength
(min=0, max=100, message=None)[source]¶ Bases:
campos.validators.Validator
String length validation.
Parameters: - min (
int
) – minimum valid length, defaults to 0 - max (
int
) – maximum valid length, defaults to 100 - message (
str
) – message to show ifnot min <= len(value) <= max
- min (
-
class
campos.validators.
RegExp
(exp, flags=0, message="Value doesn't match pattern")[source]¶ Bases:
campos.validators.Validator
Validate text against a regular expression.
Parameters: - exp (
str
or compiled re) – string or compiled regular expression - flags (
int
) – flags to pass tore.compile()
if it’s necessary - message (
str
) – message to show if value doesn’t fully match the regular expression
- exp (
-
class
campos.validators.
DateRange
(min=datetime.date(2016, 12, 7), max=datetime.date(9999, 12, 31), message=None)[source]¶ Bases:
campos.validators.Validator
Range validation for
datetime.date
objects.Parameters: - min (
datetime.date
) – minimum valid date, defaults to the current date - max (
datetime.date
) – maximum valid date, defaults to date.max - message (
str
) – message to show ifnot min <= value <= max
- min (
-
class
campos.validators.
TimeRange
(min=datetime.time(0, 0), max=datetime.time(23, 59, 59, 999999), message=None)[source]¶ Bases:
campos.validators.Validator
Range validation for
datetime.time
objects.Parameters: - min (
datetime.time
) – minimum valid time, defaults todatetime.time.min
- max – maximum valid time, defaults to
datetime.time.max
- message (
str
) – message to show ifnot min <= value <= max
- min (
-
class
campos.validators.
DatetimeRange
(min=datetime.datetime(1, 1, 1, 0, 0), max=datetime.datetime(9999, 12, 31, 23, 59, 59, 999999), message=None)[source]¶ Bases:
campos.validators.Validator
Range validation for
datetime.datetime
objects.Parameters: - min (
datetime.datetime
) – minimum valid datetime, defaults todatetime.datetime.min
- max – maximum valid datetime, defaults to
datetime.datetime.max
- message (
str
) – message to show ifnot min <= value <= max
- min (