Skip to main content

Python Validator

Project description

Validator

Validator is a Python library for dealing with request validating.

Table of Contents

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

Validating Arrays

Validator comes with validate_many() function, which validates multiple requests. Function takes list of requests and one rule. This rule is checked for all the requests. If one or more requests fail validation function returns False, otherwise (if all pass) True. For more details see example below:

Validation Passes:

from validator import validate_many

requests = [{"name": "Jon"},
            {"name": "Rob"},
            {"name": "Tom"},
            {"name": "Greg"}]
rule = {"name": 'required|min:3'}

result = validate_many(requests, rule) # True

We can also ahve a look at failde validations and error messages. validate_many() takes third argument as boolean, indicating return of error messages.

Validation Fails:

from validator import validate_many

requests = [{"name": "Jon"},
            {"name": ""},
            {"name": "Yo"},
            {"name": "Greg"}]
rule = {"name": 'required|min:3'}

result, errors = validate_many(requests, rule, return_errors=True)
"""
result = False
errors = [{},
          {'name': {'Min': 'Expected Maximum: 3, Got: 0', 'Required': 'Field was empty'}},
          {'name': {'Min': 'Expected Maximum: 3, Got: 2'}},
          {}]
"""

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

Rules Interconnection

Rules can affect each other. Let's take a look at Size rule. It takes 1 argument and checks if data is equal to given value (example: 'size:10').

  • Case 1: checks for length of '18' to be 18. len('18') is 2, therefore it is False.
reqs = {'age' : '18'}
rule = {'age' : 'size:18'}

validate(reqs, rule)
"""
result = False
errors = {'age': {'Size': 'Expected Size:18, Got:2'}}
"""
  • Case 2: checks if int representation of '18' is equal to 18. (int('18') = 18), therefore it is True.
reqs = {'age' : '18'}
rule = {'age' : 'integer|size:18'}

validate(reqs, rule) # True

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

Uploaded Source

Built Distribution

validator-0.1.4-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: validator-0.1.4.tar.gz
  • Upload date:
  • Size: 26.0 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.4.tar.gz
Algorithm Hash digest
SHA256 c898b13f914a1f2972580ea48b4aa41fb6d41c5d6e7886ecdec372e51a3d1359
MD5 57d2cdcb043e65291aa77634c7f66c04
BLAKE2b-256 932760dc447a08c99b699da9f38bf65f29490905a22877dc9755cb3ce5d3a252

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: validator-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 14.8 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 95bf98f7e099d7bb830171299cce555d970ce25b9368c3f6f34ad4bc1a1cedd2
MD5 b57a3b0215d82ea3e98d65cd546268d7
BLAKE2b-256 62b4768d2705e3a81a5604e1badda21139fd0f77ae31bffcae1bc1ede98f6e8c

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