Skip to main content

KWCHECKER a verifier and sanitizer for python kwargs parameters

exploits of a mom also see explain-xkcd

(fun fact: email addresses can have a apostrophe in them - so that O'Connell will be happy, however that's not a problem, as the sign is allowed only in the first part of the address, and you can't have spaces in the address either. Still: better use sql prepared statements, to be on the safe side)

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()

validation/modifier action on all parameters

You can add actions on all parameters with the on_all_pre and on_all_post parameters. on_all_pre`` defines checks and validators that are run before any of the validators defined for any one of the parameters. on_all_post``` defines checks and validators that are run after any one of the validators defined for any one of the parameters.

Here is an example that users on_all_post, to strip leading and trailing spaces on all string values.

        def func_to_test(**kwargs):
            checker = kwchecker.KwArgsChecker(required={
                    "first_name": (
                        str,
                        kwchecker.no_regex_validator("^\s*$", "Error: empty first name"),
                    ),
                    "last_name": (
                        str,
                        kwchecker.no_regex_validator(r"^\s*$", "Error: empty last name"),
                                            ),

                    "title" : str,

                    "email" : kwchecker.email_validator(),

                    "phone" : (
                        kwchecker.regex_validator(r"^[0-9\+\-,\ \(\)#]*$", "not a vald phone number")
                        )
                },
                on_all_post = (kwchecker.strip_leading_trailing_space(), kwchecker.capitalize_first_letter()) 
            )

            checker.validate(kwargs)

Installation instructions

pip install kwchecker

Also see the project here kwchecker

Metadata

Release files for kwchecker 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kwchecker 1.1.0
File Size Uploaded
kwchecker-1.1.0.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kwchecker 1.1.0
File Interpreter ABI Platform
kwchecker-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / kwchecker-1.1.0.tar.gz

Download URL kwchecker-1.1.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4cf18a50c4b17cbe3d040e3c6300a7d0b57d8ca1340b267c49a8aa4dcb6e1e6d
BLAKE2b-256 checksum
How to use checksums
ed81560ce545a7e495221343a4fdb50c9f4af42f56026fc265842f1547792aa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 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.1.0-py3-none-any.whl

Download URL kwchecker-1.1.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89bb72f372f8d4e5351f07d981800c841edc4a4b6ae55a25ca68920a50a5230b
BLAKE2b-256 checksum
How to use checksums
5c0f3fe647fbd79939d1b4b8c5ff0ef082be086c4bda171f241f77f63388923b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.5.0 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 history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page