Validators¶
This module provides some validators and validator factories, intended mainly
for use in the validate parameter of Field
s.
-
norman.validate.
ifset
(func)¶ Return a
Field
validator returningfunc(value)
if value is notNotSet
. If value isNotSet
, then it is returned andfunc
is never called. This is normally used as a wrapper around another validator to permitNotSet
values to pass. For example:>>> validator = ifset(istype(float)) >>> validator(4.3) 4.3 >>> validator(NotSet) NotSet >>> validator(None) Traceback (most recent call last): ... ValidationError: None
-
norman.validate.
isfalse
(func[, default])¶ Return a
Field
validator which passes if func returnsFalse
.Parameters: - func – A callable which returns
False
if the value passes. - default – The value to return if func returns
True
. If this is omitted, aValidationError
is raised.
- func – A callable which returns
-
norman.validate.
istrue
(func[, default])¶ Return a
Field
validator which passes if func returnsTrue
.Parameters: - func – A callable which returns
True
if the value passes. - default – The value to return if func returns
False
. If this is omitted, aValidationError
is raised.
- func – A callable which returns
-
norman.validate.
istype
(t[, t2[, t3[, ...]]])¶ Return a validator which raises a
ValidationError
on an invalid type.Parameters: t – The expected type, or types.
-
norman.validate.
map
(mapping)¶ Return a validator which maps values to new values.
Parameters: mapping – A dict mapping old values to new values. If a value is passed which has no mapping then it is accepted unchanged. For example:
>>> validator = map({1: 'one', 0: NotSet}) >>> validator(1) 'one' >>> validator(0) NotSet >>> validator(2) 2
-
norman.validate.
settype
(t, default)¶ Return a
Field
validator which converts the value to type t.Parameters: - t – The required type.
- default – If the value cannot be converted, then use this value instead.
The following three functions return validators which convert a value to
a datetime
object using a format string. See
strftime() and strptime() Behavior for more information of format strings.
-
norman.validate.
todate
([fmt])¶ Return a validator which converts a string to a
datetime.date
. If fmt is omitted, the ISO representation used bydatetime.date.__str__
is used, otherwise it should be a format string fordatetime.datetime.strptime
.If the value passed to the validator is a
datetime.datetime
, the date component is returned. If it is adatetime.date
it is returned unchanged.The return value is always a
datetime.date
object. If the value cannot be converted aValidationError
is raised.
-
norman.validate.
todatetime
([fmt])¶ Return a validator which converts a string to a
datetime.datetime
. If fmt is omitted, the ISO representation used bydatetime.datetime.__str__
is used, otherwise it should be a format string fordatetime.datetime.strptime
.If the value passed to the validator is a
datetime.datetime
it is returned unchanged. If it is adatetime.date
ordatetime.time
, it is converted to adatetime.datetime
, replacing missing the missing information with1900-1-1
or00:00:00
.The return value is always a
datetime.datetime
object. If the value cannot be converted aValidationError
is raised.
-
norman.validate.
totime
([fmt])¶ Return a validator which converts a string to a
datetime.time
. If fmt is omitted, the ISO representation used bydatetime.time.__str__
is used, otherwise it should be a format string fordatetime.datetime.strptime
.If the value passed to the validator is a
datetime.datetime
, the time component is returned. If it is adatetime.time
it is returned unchanged.The return value is always a
datetime.time
object. If the value cannot be converted aValidationError
is raised.