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.6.tar.gz
(4.2 kB
view details)
Built Distribution
File details
Details for the file kanpai-0.1.6.tar.gz
.
File metadata
- Download URL: kanpai-0.1.6.tar.gz
- Upload date:
- Size: 4.2 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.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3afcaba9ac5251cd3720701b93e5fc522c89a1c91bf3e688ecb76eb49267e1e |
|
MD5 | b99b7407dab7073f7c2d4157c11aee16 |
|
BLAKE2b-256 | 54e49fdb543219ab16e2aec2c5584b2980ce41433619f4f836f0e9800ade06c4 |
File details
Details for the file kanpai-0.1.6-py3-none-any.whl
.
File metadata
- Download URL: kanpai-0.1.6-py3-none-any.whl
- Upload date:
- Size: 7.3 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.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15c26718c5916c6b0c92df2cc256640b67f8fe127ff39eadf8a6888813a63bd3 |
|
MD5 | 674371f36ce92f2c2c25e86d9675d52e |
|
BLAKE2b-256 | 7f5b39a601b3676b610671a636c5b570c02da1e53e3a8b14b29b755411820a7a |