Skip to main content

Python Validator

Project description

Validator

Validator is a Python library for dealing with request validating.

Installation

Use the package manager pip to install Validator.

pip install validator

Usage

User should pass request dictionary and rules dictionary for validating data in the request.

Please see examples below:

from validator import validate

reqs = {"name": "Jon Doe",
        "age": 33,
        "mail": "jon_doe@gmail.com"}

rule = {"name": "required",
        "age": "integer|min:18",
        "mail": "required|mail"}

result = validate(request, rules) # True

valiadte() returns either True or False.

Another option is to use Validator class

from validator import Validator

reqs = {...}
rule = {...}

val = Validator(request, rules)
result = val.validate() # True

Error Messages

Validator allows user to have a look at failed validations

from validator import validate

reqs = {"name": "",
        "mail": "jon_doe"}

rule = {"name": "required",
        "mail": "mail"}

result, errors = validate(reqs, rule, return_errors=True)

"""
result = True
errors = {'name': {'Required': 'Field was empty'},
          mail': {'Mail': 'Expected a Mail, Got: jon_doe'}}
"""

Or you can use Validator class for error messages as well (result and errors are same).

val = Validator(request, rules)
result = val.validate()
errors = val.get_error_messages()

Rules

Validator Rules can be used in different ways. Please see some examples below:

Strings

rule = {"name": "required",
        "age": "integer|min:18",
        "mail": "required|mail"}

Array of Strings

rule = {"name": ["required"],
        "age": ["integer", "min:18"],
        "mail": ["required", "mail"]}

Array of Rules

from validator import rules as R

rules = {"name": [R.Required()],
        "age": [R.Integer(), R.Min(18)],
        "mail": [R.Requried(), R.Mail()]}

Other Miscellaneous

from validator import rules as R

rules = {"name": R.Required(),           # no need for Array Brackets if one rule
        "age": [R.Integer, R.Min(18)],
        "mail": [R.Requried, R.Mail]}   # no need for class initialization with brakcets () 
                                        # if no arguments are passed to rule

All of rules are listed in RULES.md file

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please see CONTRIBUTING.md before making PR :)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

validator-0.1.1.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

validator-0.1.1-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file validator-0.1.1.tar.gz.

File metadata

  • Download URL: validator-0.1.1.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for validator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6cd4b19efdd8d1f663b75324b4c059270f506de4de0e2985521b852d5c94a063
MD5 da5a724b72f9ed77884e45f7ab9e617c
BLAKE2b-256 3fbf32b11f4d26faec8101b68465761870bbf1d19395832f464fa44cde352cf1

See more details on using hashes here.

Provenance

File details

Details for the file validator-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: validator-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for validator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 18386c77ff53832a2ff88322d6f09f2535b6655584da16d20d828c7a94933c79
MD5 36ce0ffaeeef4aff5d27c4e54ea91b94
BLAKE2b-256 6525ddc367e1b6bbb4d2a6d098cbfa2b129d4b638b68460acb76fd004e234c75

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page