Skip to main content

kanpai is a library for validating Python data structures, mainly those converted from JSON. e.g. JSON received from api request, obtained from config file etc. The library is built with a focus on better error message. e.g. when validating a dict(which may be converted from JSON), in case of error, Kanpai returns a dict with error details against each keys

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
  }
}

Installation

Use pip

pip install kanpai

Validators

Validators are the building blocks for Kanpai library. There are validators for different types of data. e.g. String, Array, Object etc. All validators are accessible from Kanpai namespace.

from kanpai import Kanpai

Kanpai.String()
Kanpai.Array()
Kanpai.Boolean()

Validation rules can be applied on a validator instance by calling rule methods. All rule method returns self. So method calls can be chained. During validaton the rules are checked in order they are applied during validator construction.

Every validator returns an instance of Validator class which has a method called validate. Validate takes data to be validated as input and return a dictionary obejct containing:

{
 'success':'Boolean - Whether validation is success or not',
 'error': 'validation error',
 'data':'Incase of error data provided for validation, in case success validated data
}

After creating a validator it can be configured to apply validation rule by calling its rule methods. After constructing a validator it can be used for multiple validation safely.

For more details on individual validators and its rule methods please refer corresponding file in docs folder.

Metadata

Release files for kanpai 0.1.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kanpai 0.1.15
File Size Uploaded
kanpai-0.1.15.tar.gz 12.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kanpai 0.1.15
File Interpreter ABI Platform
kanpai-0.1.15-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / kanpai-0.1.15.tar.gz

Download URL kanpai-0.1.15.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f10777b3bf57ac05b59bb5cefbabf7d8c322345faccd4bd3f953c61467183892
BLAKE2b-256 checksum
How to use checksums
a1b1e56c792f2cee131003803f5bb7cd600e095a447158191dc74705273314d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.7

Release files / kanpai-0.1.15-py3-none-any.whl

Download URL kanpai-0.1.15-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b8d60de55a6c13fbb79ab978b69095751ad55e2d35c3f7eb4d2f236cf85abd4f
BLAKE2b-256 checksum
How to use checksums
f36edd30dc8f6f64fb17b159453c0b331f8576cd69c28e2bafa4f70ec8c840fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.7

Release history Release notifications | RSS feed

This release

0.1.15 This release

2 release files

0.1.13

1 release file

0.1.12

1 release file

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page