Python JSON schema validator with better error message
Project description
kanpai is a library for validating Python data structures, mainly those coverted from JSON. e.g. JSON received from api request, obtained from config file etc.
Example
Here is a quick example
import kanpai as Kanpai
schema = Kanpai.Object({
'first_name': (Kanpai.String(error='User first name must be string.')
.trim()
.required(error='Please provide user first name.')
.max(256, error='Maximum allowed length is 256')),
'last_name': (Kanpai.String(error='User last name must be a String')
.trim()
.required(error='Please provide user last name.')),
'age' : (Kanpai.Number(error='Age must be a number.')
.max(35,'Maximum allowed age is 35')
.min(18,'Age must be minimum 18'))
})
validation_result = schema.validate({
'first_name':'Chandrakanta',
'age': 15
})
assert validation_result == {
'success': False,
'error': {
'last_name': 'Please provide user last name.',
'age': 'Age must be minimum 18'
},
'data': {
'first_name':'Chandrakanta',
'age': 15
}
}
schema.validate
return a dictionary obejct containing
{
'success':'Whether validation is success or not',
'error':'Validation error',
'data':'Incase of error data provided for validation , in case success validated data'
}
Installation
Use pip
pip install kanpai
Test
pytest # to run tests
pytest --cov-report=html --cov-branch --cov=kanpai # to generate coverage report
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
kanpai-0.1.2.tar.gz
(3.5 kB
view details)
Built Distribution
File details
Details for the file kanpai-0.1.2.tar.gz
.
File metadata
- Download URL: kanpai-0.1.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30e3f262f5ef9037e9f72e09c225a2b4267bfb7d63defc2a70d7f3ccf3a96cee |
|
MD5 | 867ebcb3b47086b95ebf0ba9287ee835 |
|
BLAKE2b-256 | c848a52391be8a3c31c9f75c6f46fb610c2b346fc855ec57df6085a178b202f0 |
File details
Details for the file kanpai-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: kanpai-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2aa9e306acc072c3996717c18147a328da4f60940570cddf83dcb50f80640a61 |
|
MD5 | 920908b8e55fcc6cbe7b9b70b1d9d409 |
|
BLAKE2b-256 | cd6143322f773cec75cc2471d0797af7d4b39dcf8fcf41e0a35f3e6365668425 |