api-validation package.
Project description
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.
Example: Validate literals
from api.validations import Validation
print(Validation.validate("123", int, "user")) # (123, True)
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)
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
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ApiValidations-1.0.0.tar.gz
.
File metadata
- Download URL: ApiValidations-1.0.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ee60117e7f0d34e9d11c0d957ea7ec9978b49fe33fe61bb09eb93d21ac9054d |
|
MD5 | a5a4c8a0ced8d98bb1044d08aa0018b4 |
|
BLAKE2b-256 | 8952d60b9220047c3382f8ee62fe36e3487aab30185f34d914eae0e1b12648aa |
File details
Details for the file ApiValidations-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: ApiValidations-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f31bf142f80f5bccc9c9a2592269bd5f28a5d5b7e53a75afd0433b8f7ea78ac6 |
|
MD5 | 2bab5ebee96f18ca982911d83efb2022 |
|
BLAKE2b-256 | 69d4e472616d147feb0043fbe59129cb00650ebafbd088379361161ce6ce82fa |