Skip to main content

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

from kanpai import 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


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.14.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

kanpai-0.1.14-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file kanpai-0.1.14.tar.gz.

File metadata

  • Download URL: kanpai-0.1.14.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.7

File hashes

Hashes for kanpai-0.1.14.tar.gz
Algorithm Hash digest
SHA256 99d5f7a404c9be5d67b3e7c4d0b44ddcb4833d68ecf73bfb6b9b3bff43b53cd6
MD5 8b2abd01b91cfed29bf7e299943bb332
BLAKE2b-256 2c9c231a3746154617de86b9c7ddf4e78747730c331c56e4909c2ec50b8cb311

See more details on using hashes here.

File details

Details for the file kanpai-0.1.14-py3-none-any.whl.

File metadata

  • Download URL: kanpai-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.7

File hashes

Hashes for kanpai-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 bab1f74b3ea89906518d48bd2df1a375a70ca71b06e9168b8e77f10be561257d
MD5 7b4d67218df6bad4644c7a81c9c73d8e
BLAKE2b-256 5145c8da9cb4cae9b6c3a2809ba1e9f08da0b7a62346cbe8316152828071eed4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page