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

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


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

Uploaded Source

Built Distribution

kanpai-0.1.5-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kanpai-0.1.5.tar.gz
  • Upload date:
  • Size: 4.0 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

Hashes for kanpai-0.1.5.tar.gz
Algorithm Hash digest
SHA256 e263412dad8d107f8dbdcb91db34b9451e636197017f2f626351a40e273e1cba
MD5 94f45acc004e5fdb3b38b82e6e403527
BLAKE2b-256 67d7371bb8d5cfd80294a8a3befe756cfe35f6d372b97c073f9653d6abc55103

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kanpai-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 6.8 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

Hashes for kanpai-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 84ca82ecf78f04188399c9a836b696c55035562599048f0ea51a6c4f95f503a4
MD5 c0e3d999326d0cdcc0266b6fa087f949
BLAKE2b-256 65cd6c24f2fc04613f1b9dc283d82685fec0a38bb6b5195446c48fa520908c64

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