Skip to main content

This is a python-package that makes data-validation easier for python developers. It provides interfaces like Field, IterableField, Validaion etc for validation.

Quick Start

In order to use this library, you first need to go through the following steps:

Installation

Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.

With virtualenv, it’s possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.

Supported Python Versions

Python >= 3.5

Mac/Linux

$ pip install virtualenv
$ virtualenv venv -p python3
$ . venv/bin/activate
$ source venv/bin/activate
$ pip install git+https://github.com/soni-vikas/api-validaion.git#egg=api-validation'

Example Usage

Validation.validate:

It will return tuple of length 2: validated_payload, error_flag, validation_status In case of any validation failure, the first value will be the error message & the second value will be True. In case there are no errors, the first value will be the validated payload, the second value will be False

Field: describes how to validate a field.

IteratorField: subclass of Field, used for a list or any other iterator.

  1. Example: Validate literals

from api.validations import Validation

print(Validation.validate("123", int, "user")) # (123, True)
  1. Example: Custom validation

from api.validations import Validation
def _check_in_list(x, ls):
    if x not in ls:
        raise ValueError()

    return x

device = ["cpu", "gpu"]
print(Validation.validate("cpu", lambda x: _check_in_list(x, device), "device"))
# ('cpu', False)

print(Validation.validate("amd", lambda x: _check_in_list(x, device), "device"))
# ("Field 'device' is in an invalid format.", True)
  1. Example: Validation for iterables using Field

from api.validations import Validation
from api.validations import Field, IterableField
import re

employee = {
    "name": "vikas soni",
    "phone": "8080808080",
    "org_ids": [
        123,
        345
    ]
}

validation_dict = {
    "name": Field(required=True, pattern=re.compile("[a-z]+( [a-z]+)*"), null=True),
    "phone": Field(required=True, pattern=re.compile("^[1-9][0-9]{9}$"), null=True),
    "org_ids": IterableField(required=True, sub_pattern=int)
}
payload, error = Validation.validate(employee, validation_dict)
print(payload)
print(error)

# {'name': 'vikas soni', 'phone': '8080808080', 'org_ids': [123, 345]}
# False
  1. Example: Validation for iterables using JSON schema

from api.validations import Validation
import re

employee = {
    "name": "vikas soni",
    "phone": "8080808080",
    "org_ids": [
        123,
        345
    ]
}

validation_dict = {
    "name": {
        'pattern': re.compile(r'[a-z]+( [a-z]+)*'),
        'required': True,
        'null': True
    },
    "phone": {
        'pattern': re.compile("^[1-9][0-9]{9}$"),
        'required': True,
        'null': True
    },
    "org_ids": {
        'pattern': list,
        'required': True,
        'null': False,
        'sub_pattern': int
    }
}
payload, error = Validation.validate(employee, validation_dict)
print(payload)
print(error)

# {'name': 'vikas soni', 'phone': '8080808080', 'org_ids': [123, 345]}
# False

for more examples, see tests cases available in tests/

Development

Getting Started

Assuming that you have Python and virtualenv installed, set up your environment and install the required dependencies defined above:

$ git clone https://github.com/soni-vikas/api-validaion.git
$ cd api-validation
$ virtualenv venv -p python3
...
$ . venv/bin/activate
$ pip install -e .

Running Tests

You can run tests in all supported Python versions using python setup.py test. By default, it will run all of the unit and functional tests.

$ python setup.py test

You can also run individual tests with your default Python version: see `--help`.

$ python setup.py test --help

For any query raise an issue or create a pull request.

Release files for ApiValidations 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ApiValidations 1.0.0
File Size Uploaded
ApiValidations-1.0.0.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ApiValidations 1.0.0
File Interpreter ABI Platform
ApiValidations-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / ApiValidations-1.0.0.tar.gz

Download URL ApiValidations-1.0.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1ee60117e7f0d34e9d11c0d957ea7ec9978b49fe33fe61bb09eb93d21ac9054d
BLAKE2b-256 checksum
How to use checksums
8952d60b9220047c3382f8ee62fe36e3487aab30185f34d914eae0e1b12648aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

Release files / ApiValidations-1.0.0-py3-none-any.whl

Download URL ApiValidations-1.0.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f31bf142f80f5bccc9c9a2592269bd5f28a5d5b7e53a75afd0433b8f7ea78ac6
BLAKE2b-256 checksum
How to use checksums
69d4e472616d147feb0043fbe59129cb00650ebafbd088379361161ce6ce82fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page