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

Uploaded Source

Built Distribution

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

Uploaded Python 3

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