validation - A small collection of validation rules
- Purpose:
This module serves as a general validation processor.
Note
This is currently very basic module, and only contains validation rules which are commonly used by S3DEV projects. More functionality will be added as needed.
- Platform:
Linux/Windows | Python 3.6+
- Developer:
J Berendt
- Email:
- Comments:
n/a
- Example:
Test if an IP address is valid:
>>> from utils4 import validation >>> validation.rules.is_ip(value='128.31.0.62') True
Test if a set of geo coordinates is valid:
>>> from utils4 import validation >>> validation.rules.is_coordinates(value=['4.6097100', '-74.0817500']) True
Return the regex pattern for latitude:
>>> from utils4 import validation >>> validation.regex.latitude re.compile(r'^(-)?(([0-9]|[1-8][0-9])(\.[0-9]{1,})?|90)$', re.UNICODE)
- class validation.RegEx[source]
This class holds a variety of validation regex strings.
Each property in this class returns a compiled regex pattern, of the type
re.Pattern
.- property latitude
Regex string for latitude.
- Returns:
A compiled regex pattern for latitude.
- Return type:
re.Pattern
- property longitude
Regex string for longitude.
- Returns:
A compiled regex pattern for longitude.
- Return type:
re.Pattern
- class validation.Rules[source]
A collection of validation rules.
- is_coordinates(value: List[str]) bool [source]
Test
value
is a valid set of geo coordinates.- Parameters:
value (List[str]) – A list containing latitude and longitude as strings.
- Rules:
value
is alist
The index 0 is a valid latitude coordinate (-90, 90)
The index 1 is a valid longitude coordinate (-180, 180)
For example:
['4.6097100', '-74.0817500']
- Returns:
True if
value
is a valid set of coordinates, otherwise False.- Return type:
bool
- static is_ip(value: str) bool [source]
Test if an IP address (IPv4 or IPv6) is valid.
This function wraps the built-in
ipaddress.ip_address()
function to test whether an IP address is valid, or not.- Parameters:
value (str) – The IP address to be tested.
- Returns:
True if
value
is a valid IPv4 or IPv6 address, otherwise False.- Return type:
bool
- is_latitude(value: str) bool [source]
Test
value
is a latitude coordinate (-90, 90).- Parameters:
value (str) – The latitude coordinate to be tested.
- Returns:
True if
value
is a valid latitude coordinate, otherwise False.- Return type:
bool
- static is_list(value: object, indexes: int = None) bool [source]
Validate if
value
is alist
with (n) number of indexes.- Parameters:
value (list) – The object to be tested.
indexes (int, optional) – Number of expected indexes in the list. Defaults to None.
- Returns:
True if
value
is alist
with (n) indexes, otherwise False.- Return type:
bool