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.2.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: validator-0.1.2.tar.gz
  • Upload date:
  • Size: 22.2 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.2.tar.gz
Algorithm Hash digest
SHA256 628f17f1b84cd84d3c5d91448c17ac3e8b35438c51fe4cdb0fc764c22a76d00f
MD5 29cd5ddd82337f32a221394e14bc8532
BLAKE2b-256 3a8001d0da6303950f6188053911a517f88f1cebf4370c794b7dbf613ca27d44

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: validator-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2cbd8d91b8c34e27ee57316fc51c7becae8a1cafc32e397a5e6c412ad7ba38ee
MD5 e22f6da05147182b0514da0be6748429
BLAKE2b-256 1d01ae6bde012a2e608a59a04d08fb210f88ee987a2ce2ea9a33105713db51da

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