Validator Reference¶
| Core | Date/Time | Numbers | File-related | Internet-related |
|---|---|---|---|---|
dict |
date |
numeric |
bytesIO |
email |
string |
datetime |
integer |
stringIO |
url |
iterable |
time |
float |
path |
ip_address |
none |
timezone |
fraction |
path_exists |
ipv4 |
not_empty |
decimal |
file_exists |
ipv6 |
|
uuid |
directory_exists |
mac_address |
||
variable_name |
Core¶
dict¶
-
dict(value, allow_empty=False, json_serializer=None)[source]¶ Validate that
valueis adict.Hint
If
valueis a string, this validator will assume it is a JSON object and try to convert it into adictYou can override the JSON serializer used by passing it to the
json_serializerproperty. By default, will utilize the Pythonjsonencoder/decoder.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse. - json_serializer (callable) – The JSON encoder/decoder to use to deserialize a
string passed in
value. If not supplied, will default to the Pythonjsonencoder/decoder.
Returns: value/NoneReturn type: dict/NoneRaises: - ValueError – if
valueis empty andallow_emptyisFalse - ValueError – if
valueis not adict
string¶
-
string(value, allow_empty=False, coerce_value=False, minimum_length=None, maximum_length=None, whitespace_padding=False)[source]¶ Validate that
valueis a valid string.Parameters: - value (
str/None) – The value to validate. - allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse. - coerce_value (
bool) – IfTrue, will attempt to coercevalueto a string if it is not already. IfFalse, will raise aValueErrorifvalueis not a string. Defaults toFalse. - minimum_length (
int) – If supplied, indicates the minimum number of characters needed to be valid. - maximum_length (
int) – If supplied, indicates the minimum number of characters needed to be valid. - whitespace_padding (
bool) – IfTrueand the value is below theminimum_length, pad the value with spaces. Defaults toFalse.
Returns: value/NoneReturn type: str/NoneRaises: - ValueError – if
valueis empty andallow_emptyisFalse - ValueError – if
valueis not a valid string andcoerce_valueisFalse - ValueError – if
minimum_lengthis supplied and the length ofvalueis less thanminimum_lengthandwhitespace_paddingisFalse - ValueError – if
maximum_lengthis supplied and the length ofvalueis more than themaximum_length
- value (
iterable¶
-
iterable(value, allow_empty=False, forbid_literals=(<class 'str'>, <class 'bytes'>), minimum_length=None, maximum_length=None)[source]¶ Validate that
valueis a valid iterable.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse. - forbid_literals (iterable) – A collection of literals that will be considered invalid
even if they are (actually) iterable. Defaults to
strandbytes. - minimum_length (
int) – If supplied, indicates the minimum number of members needed to be valid. - maximum_length (
int) – If supplied, indicates the minimum number of members needed to be valid.
Returns: value/NoneReturn type: iterable /
NoneRaises: - ValueError – if
valueis empty andallow_emptyisFalse - ValueError – if
valueis not a valid iterable orNone - ValueError – if
minimum_lengthis supplied and the length ofvalueis less thanminimum_lengthandwhitespace_paddingisFalse - ValueError – if
maximum_lengthis supplied and the length ofvalueis more than themaximum_length
none¶
-
none(value, allow_empty=False)[source]¶ Validate that
valueisNone.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty but notNone. IfFalse, raises aValueErrorifvalueis empty but notNone. Defaults toFalse.
Returns: NoneRaises: ValueError – if
allow_emptyisFalseandvalueis empty but notNoneand
not_empty¶
-
not_empty(value, allow_empty=False)[source]¶ Validate that
valueis not empty.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse.
Returns: value/NoneRaises: ValueError – if
valueis empty andallow_emptyisFalse
uuid¶
-
uuid(value, allow_empty=False)[source]¶ Validate that
valueis a validUUID.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse.
Returns: valuecoerced to aUUIDobject /NoneReturn type: UUID/NoneRaises: - ValueError – if
valueis empty andallow_emptyisFalse - TypeError – if
valuecannot be coerced to aUUID
variable_name¶
-
variable_name(value, allow_empty=False)[source]¶ Validate that the value is a valid Python variable name.
Caution
This function does NOT check whether the variable exists. It only checks that the
valuewould work as a Python variable (or class, or function, etc.) name.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse.
Returns: value/NoneReturn type: strorNoneRaises: ValueError – if
allow_emptyisFalseandvalueis empty
Date / Time¶
date¶
-
date(value, allow_empty=False, minimum=None, maximum=None)[source]¶ Validate that
valueis a valid date.Parameters: - value (
str/datetime/date/None) – The value to validate. - allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse. - minimum (
datetime/date/ compliantstr/None) – If supplied, will make sure thatvalueis on or after this value. - maximum (
datetime/date/ compliantstr/None) – If supplied, will make sure thatvalueis on or before this value.
Returns: value/NoneReturn type: date/NoneRaises: - ValueError – if
valueis empty andallow_emptyisFalse - ValueError – if
valueis not a valid value type orNone - ValueError – if
minimumis supplied butvalueoccurs beforeminimum - ValueError – if
maximumis supplied butvalueoccurs afterminimum
- value (
datetime¶
-
datetime(value, allow_empty=False, minimum=None, maximum=None)[source]¶ Validate that
valueis a valid datetime.Caution
If supplying a string, the string needs to be in an ISO 8601-format to pass validation. If it is not in an ISO 8601-format, validation will fail.
Parameters: - value (
str/datetime/date/None) – The value to validate. - allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse. - minimum (
datetime/date/ compliantstr/None) – If supplied, will make sure thatvalueis on or after this value. - maximum (
datetime/date/ compliantstr/None) – If supplied, will make sure thatvalueis on or before this value.
Returns: value/NoneReturn type: datetime/NoneRaises: - ValueError – if
valueis empty andallow_emptyisFalse - ValueError – if
minimumis supplied butvalueoccurs beforeminimum - ValueError – if
maximumis supplied butvalueoccurs afterminimum
- value (
time¶
-
time(value, allow_empty=False, minimum=None, maximum=None)[source]¶ Validate that
valueis a validtime.Caution
This validator will always return the time as timezone naive (effectively UTC). If
valuehas a timezone / UTC offset applied, the validator will coerce the value returned back to UTC.Parameters: - value (
datetimeortime-compliantstr/datetime/time) – The value to validate. - allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse. - minimum (
datetimeortime-compliantstr/datetime/time) – If supplied, will make sure thatvalueis on or after this value. - maximum (
datetimeortime-compliantstr/datetime/time) – If supplied, will make sure thatvalueis on or before this value.
Returns: valuein UTC time /NoneReturn type: time/NoneRaises: - ValueError – if
valueis empty andallow_emptyisFalse - ValueError – if
valueis not a valid value type orNone - ValueError – if
minimumis supplied butvalueoccurs beforeminimum - ValueError – if
maximumis supplied butvalueoccurs afterminimum
- value (
timezone¶
-
timezone(value, allow_empty=False, positive=True)[source]¶ Validate that
valueis a validtzinfo.Caution
This does not validate whether the value is a timezone that actually exists, nor can it resolve timzone names (e.g.
'Eastern'or'CET').For that kind of functionality, we recommend you utilize: pytz
Parameters: - value (
str/tzinfo/ numeric /None) – The value to validate. - allow_empty (
bool) – IfTrue, returnsNoneifvalueis empty. IfFalse, raises aValueErrorifvalueis empty. Defaults toFalse. - positive (
bool) – Indicates whether thevalueis positive or negative (only has meaning ifvalueis a string). Defaults toTrue.
Returns: value/NoneReturn type: tzinfo/NoneRaises: - ValueError – if
valueis empty andallow_emptyisFalse - ValueError – if
valueis not a valid value type orNone
- value (
Numbers¶
Note
Because Python’s None is implemented as an integer
value, numeric validators do not check “falsiness”. Doing so would find
false positives if value were set to 0.
Instead, all numeric validators explicitly check for the Python global singleton
None.
numeric¶
-
numeric(value, allow_empty=False, minimum=None, maximum=None)[source]¶ Validate that
valueis a numeric value.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueisNone. IfFalse, raises aValueErrorifvalueisNone. Defaults toFalse. - minimum (numeric) – If supplied, will make sure that
valueis greater than or equal to this value. - maximum (numeric) – If supplied, will make sure that
valueis less than or equal to this value.
Returns: value/NoneRaises: - ValueError – if
valueisNoneandallow_emptyisFalse - ValueError – if
minimumis supplied andvalueis less than theminimum - ValueError – if
maximumis supplied andvalueis more than themaximum
integer¶
-
integer(value, allow_empty=False, coerce_value=False, minimum=None, maximum=None, base=10)[source]¶ Validate that
valueis anint.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueisNone. IfFalse, raises aValueErrorifvalueisNone. Defaults toFalse. - coerce_value (
bool) – IfTrue, will force any numericvalueto an integer (always rounding up). IfFalse, will raise an error ifvalueis numeric but not a whole number. Defaults toFalse. - minimum (numeric) – If supplied, will make sure that
valueis greater than or equal to this value. - maximum (numeric) – If supplied, will make sure that
valueis less than or equal to this value. - base – Indicates the base that is used to determine the integer value.
The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be
optionally prefixed with
0b/0B,0o/0O/0, or0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16. Defaults to10.
Returns: value/NoneRaises: - ValueError – if
valueisNoneandallow_emptyisFalse - ValueError – if
minimumis supplied andvalueis less than theminimum - ValueError – if
maximumis supplied andvalueis more than themaximum
float¶
-
float(value, allow_empty=False, minimum=None, maximum=None)[source]¶ Validate that
valueis afloat.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueisNone. IfFalse, raises aValueErrorifvalueisNone. Defaults toFalse.
Returns: value/NoneReturn type: float/NoneRaises: - ValueError – if
valueisNoneandallow_emptyisFalse - ValueError – if
minimumis supplied andvalueis less than theminimum - ValueError – if
maximumis supplied andvalueis more than themaximum
fraction¶
-
fraction(value, allow_empty=False, minimum=None, maximum=None)[source]¶ Validate that
valueis aFraction.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueisNone. IfFalse, raises aValueErrorifvalueisNone. Defaults toFalse.
Returns: value/NoneReturn type: Fraction/NoneRaises: - ValueError – if
valueisNoneandallow_emptyisFalse - ValueError – if
minimumis supplied andvalueis less than theminimum - ValueError – if
maximumis supplied andvalueis more than themaximum
decimal¶
-
decimal(value, allow_empty=False, minimum=None, maximum=None)[source]¶ Validate that
valueis aDecimal.Parameters: - value – The value to validate.
- allow_empty (
bool) – IfTrue, returnsNoneifvalueisNone. IfFalse, raises aValueErrorifvalueisNone. Defaults toFalse. - minimum (numeric) – If supplied, will make sure that
valueis greater than or equal to this value. - maximum (numeric) – If supplied, will make sure that
valueis less than or equal to this value.
Returns: value/NoneReturn type: Decimal/NoneRaises: - ValueError – if
valueisNoneandallow_emptyisFalse - ValueError – if
minimumis supplied andvalueis less than theminimum - ValueError – if
maximumis supplied andvalueis more than themaximum