Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

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

from validator import Validator


request = {
    "firstName": "Jon",
    "lastName": "Doe",
    "age": 33,
    "mail": "jon_doe@gmail.com",
}

rules = {
    "firstName": "required",
    "lastName": "required",
    "age": "required|min:18",
    "mail": "required|mail",
}

Validator(request, rules).validate()   # returns True

Rules

Between

The field under validation must have a size between the given min and max

>>> Between(2, 15).check(23)
False

>>> Between(2, 15).check(12)
True

Integer

The field under validation must be an Integer

>>> Integer().check('123')
True

>>> Integer().check('string')
False

IP

The field under validation must be an IP address.

>>> IP().check('127.0.0.1')
True

>>> IP().check('0.299.2.1')
False

IPv4

The field under validation must be an IPv4 address.

>>> IPv4().check('127.0.0.1')
True

>>> IPv4().check('0.299.2.1')
False

IPv6

The field under validation must be an IPv6 address.

>>> IPv6().check('2001:0db8:85a3:0000:0000:8a2e:0370:7334')
True

>>> IPv6().check('2001:0db8:85a3:9876:1234:8a2e')
False

List

The field under validation must be a list (Python array)

>>> List().check([1, 2, 3])
True

>>> List().check(123)
False

Mail

The field under validation must be formatted as an e-mail address

>>> Mail().check('abcd@ef.gh')
True

>>> Mail().check('aaa.com')
False

Max

The field under validation must be less than or equal to a maximum value

>>> Max(18).check(23)
False

>>> Max(18).check(15)
True

Min

The field under validation must be greater than or equal to a minimum value

>>> Min(18).check(23)
True

>>> Min(18).check(15)
False

Required

The field under validation must be present in the input data and not empty

>>> Required().check('Not Empty')
True

>>> Required().check('')
False

RequiredIf

Some Description...

>>> RequiredIf('a').check('abc')
True

>>> RequiredIf('z').check('abc')
False

Size

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value (the attribute must also have the numeric or integer rule). For an array, size corresponds to the count of the array.

>>> Size(6).check('string')
True

>>> Size(12).check('string')
False

Contributing

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

Please make sure to update tests as appropriate.

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.

Files for validator, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size validator-0.1.0-py3-none-any.whl (14.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size validator-0.1.0.tar.gz (21.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page