Validators¶
Validators for use in a cion.Field
- cion.validators.email(require_tld: bool = True, *, error_message='Must be an email') Callable[[Any], Any][source]¶
Validates an email address
There are very many ways to match email addresses, ranging from just requiring the
@symbol, to a large and slow regex.This email validator is under development, and will, for the most part, validate valid email addresses: ie, no valid email address will be rejected. However, there are a great many strings that will still match, valid email or not. If you have your own method if validating email addresses, you can do that with a custom validator.
- Parameters:
require_tld (bool) –
Whether or not to require a TLD, like, “.com”.
Example: with
require_tld=False,meizuflux@examplewill be valid. Withrequire_tld=True, it will not be, but adding a.comwill make it valid.error_message – The error message to raise if the value is not a valid email
- Return type:
Callable[[Any], Any]
- cion.validators.equal_to(value_: Any, error_message: str = 'Must be equal to {equal_to}') Callable[[Any], Any][source]¶
Validator that ensures a value is equal to a certain value
No assumptions are made about types of any kind
- Parameters:
value – The item that the value must be equal to
error_message (str) – The error message that is raised when the value does not validate properly. You can use the
equal_tovariable in the messagevalue_ (Any)
- Return type:
Callable[[Any], Any]
- cion.validators.length(minimum: int | None = None, maximum: int | None = None, *, equal_to: int | None = None, base_error_message: str = 'Length must be ', error_messages: dict[str, str] | None = None) Callable[[Any], Any][source]¶
Validates the length of a value
This can be a string, list, or something like a dictionary, or basically any iterable (anything that len(value) can be called on)
- Parameters:
minimum (int) – The minimum length that the value can be
maximum (int) – The maximum length that the value can be
equal_to (int) – A number that the length of the value must equal. Cannot be used with minimum and maximum
base_error_message (str) – The start of the error message. View source for exact example
error_messages (dict) –
A dictionary of the error messages that are allowed to be raised.
If
equal_tois specified, then theequal_tokey is used and added on afterbase_error_messageIf
minimumormaximumis specified, then the respective key is used and added on afterbase_error_messageIf both
minimumandmaximumare specified, then minimum and maximum error message is added together withandand then that is added on afterbase_error_message
- Return type:
Callable[[Any], Any]
Note
The error message implementation is a bit complicated for this validator, it is recommended that you look at the source code for this function for more information
- Returns:
InnerValidator – The inner function that is called when validating schema
- Parameters:
minimum (int | None)
maximum (int | None)
equal_to (int | None)
base_error_message (str)
error_messages (dict[str, str] | None)
- Return type:
Callable[[Any], Any]
- cion.validators.not_one_of(*values: Iterable[Any], error_message: str = 'Value must not be one of {values}') Callable[[Any], Any][source]¶
Checks if the value is not in a list of values
Used as a constraint on a field with
cion.SchemaNo assumptions are made about the type of the value- Parameters:
values (Iterable[Any]) – Any non-keyword arguments are valid values
error_message (str) – The error message that is raised The values variable can be used, and it is the values joined together by a string
- Returns:
InnerValidator – The inner function that is called when validating schema
- Return type:
Callable[[Any], Any]
- cion.validators.one_of(*values: Any, error_message: str = 'Value must be one of {values}') Callable[[Any], Any][source]¶
Checks if the value is in a list of values
Used as a constraint on a field with
cion.SchemaNo assumptions are made about the type of the value- Parameters:
values (Any) – Any non-keyword arguments are valid values
error_message (str) – The error message that is raised The values variable can be used, and it is the values joined together by a string
- Returns:
InnerValidator – The inner function that is called when validating schema
- Return type:
Callable[[Any], Any]
- cion.validators.range_(minimum: int | None = None, maximum: int | None = None, error_message: str = 'Number must be between {minimum} and {maximum}') Callable[[Any], Any][source]¶
Validates that a number is in a range
- Parameters:
minimum (int) – The minimum length that the number can be
maximum (int) – The maximum length that the number can be
error_message (str) – The error message that is raised You can use the
minimumandmaximumvariables in the error message
- Return type:
Callable[[Any], Any]
- cion.validators.regex(pattern: str, *, cast: bool = False, function: Literal['match', 'fullmatch', 'search'] = 'fullmatch', flags: Any = 0, error_message='Must match regex', **kwargs: Any) Callable[[Any], Any][source]¶
Match a value against a regex pattern
Note
It is highly recommended that you customize the error message, as regex functions tend to be application specific, and not generic.
- Parameters:
pattern (str) – The regex pattern that you want to match a string against
cast (bool) – The value is always casted to a string, but if you wish to preserve the initial value, set this to
Falsefunction (One of
match,fullmatch,search) –What regex function to use against the value. View the regex docs for information about these https://docs.python.org/3/library/re.html#re.fullmatch
TL,DR: Match matches at the start, fullmatch matches the entire string, and search searches the string for something to match
flags (Any) – Flags to pass to :meth:
re.compile. https://docs.python.org/3/library/re.html#re.Aerror_message – The error message raised when the match object returned when matching is None.
kwargs (Any) – Arguments to pass to the method used for matching (again, see regex docs related to the function)
- Return type:
Callable[[Any], Any]
- cion.validators.url(schemes: list[str] = ['http', 'https'], *, error_message: str = 'Must be a valid URL') Callable[[Any], Any][source]¶
Validates that a value is a valid URL
This implementation uses
regex()under the hood- Parameters:
schemes (list[str]) – A list of http schemes that will be valid
error_message (str) – The error message to raise if a value is not a valid URL
- Return type:
Callable[[Any], Any]