Coder Social home page Coder Social logo

kanpai's Introduction

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.

kanpai's People

Contributors

chandrakantap avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

kanpai's Issues

Required more flexibility for error messages

More flexibility is required for error messages.
Example:
for invalid email address error message should be passed manually.
The automated error message "pattern doesn't match" is too vague.

Hope this will be modified soon.

No implementation for Date, Time and DateTime

Hi, I find this project useful for a work I just completed.
But due to lack of some implementations, I have to implement them inside my project.
I will like to ask if I can refactor the code and bring in those new validators into this project.

Thank you for this work, it was really helpful.

Wanted to give this package a go but not there yet

I was looking for a validator package with little overhead and found this one. At first I thought it'd work fine but found several things needing improvements and thus was not able use it.

  1. It needs better documentation. i.e. No mention of Array().of().. had to find it by browsing the code
  2. Cannot validate booleans
  3. This is the worst, the object that is passed in to schema.validate is mutated.

how to set thresholds on previously defined schema

I've gone through and documented our json report schema as kanpai objects.
This has made sure our generated output is complete as defined (i.e. no unexpected/missing required elements).
Now I want to extend the base schema with min()/max() thresholds among other quality gates for specific run criteria.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.