KWCHECKER a verifier and sanitizer for python kwargs parameters
also see explain-xkcd
(fun fact: email addresses can have a apostrophe in them - so that O'Connell will be happy, therefore: sanitizing of an email may still leave you with an usafe string, but better use sql prepared statements)
The kwchecker package simplifies validation & sanitation of python kwargs parameter, your can define most validations declaratively, in a map.
See the following example, where the validator definitions are passed to the kwchecker.KwArgsChecker constructor.
def func_to_test(**kwargs):
checker = kwchecker.KwArgsChecker(required={
"first_name": (
str,
kwchecker.no_regex_validator("^\s*$", "Error: empty first name"),
kwchecker.strip_leading_trailing_space(),
kwchecker.capitalize_first_letter(),
),
"last_name": (
str,
kwchecker.no_regex_validator(r"^\s*$", "Error: empty last name"),
kwchecker.strip_leading_trailing_space(),
kwchecker.capitalize_first_letter(),
),
"title" : str,
"email" : kwchecker.email_validator(),
"phone" : (
kwchecker.regex_validator(r"^[0-9\+\-,\ \(\)#]*$", "not a vald phone number")
)
}, opt={
"mood": kwchecker.int_range_validator(0, 10),
"plan": str
})
checker.validate(kwargs)
The constructor has two arguments required receives the definition for mandatory parameters passed via kwargs, opt receives definitions of optional parameters.
An errror is triggered, if an actual parameter in kwargs does not appear in any of these definitions.] The constructor also has an optional sanitize_db parameter, if set to True then this will removes all ap ' and " characters.
Each allowed parameter name appears as a key in the dictionary arguments. The value of the entry defines the actual validation/sanitation. A type value will check, if the supplied argument is of the requested type
"title" : str - this will check if the kwargs parameter title is of type string.
A funcion value will be either a validation function, or a sanitier function.
"email" : kwchecker.email_validator() - this validates that the email parameter in kwargs is a valid email address. Note that function email_validator is being called here, as it returns a closure. Most of the verifiers can take additional arguments, like an error message that overrides the default one, therefore I kept with this convention for all validators and sanitizer functions, in order to make it consistent.
You may pass a tuple of values that are either types or functions. see
"last_name": (
str,
kwchecker.no_regex_validator(r"^\s*$", "Error: empty last name"),
kwchecker.strip_leading_trailing_space(),
kwchecker.capitalize_first_letter(),
)
The action for the kwargs last_name parameter will first verify that parameter to be of type string, then it will verify, that the value is not an empty string kwchecker.no_regex_validator(r"^\s*$", "Error: empty last name"), then it will strip the leading and trailing whitespaces kwchecker.strip_leading_trailing_space(), and capitalize the first letter of the value kwchecker.capitalize_first_letter()
Metadata
Release files for kwchecker 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kwchecker-1.0.3.tar.gz | 6.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kwchecker-1.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / kwchecker-1.0.3.tar.gz
| Download URL | kwchecker-1.0.3.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e68dce6cbe4318239d0516ce79c4a0833f9aef358935ce8e79c9e67381b2fc9e
|
|
BLAKE2b-256 checksum How to use checksums |
9ad043d42c81d1846a9e752ed529ded0bc97c6184df9d48d0061fd085c66172d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
|
Release files / kwchecker-1.0.3-py3-none-any.whl
| Download URL | kwchecker-1.0.3-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
593d3c67b174381300c81a7b575144ebf81a2d57b25048b2c5db15a90a8192a2
|
|
BLAKE2b-256 checksum How to use checksums |
799acda1845b2cc24c2bb4da0b06c0ee50869ea7b4074472bb6c555b7a678c3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6
|