Skip to main content

simple validator for input data (only work for python dict)

Project description

makesure

Simple validation for input datas

Installation

pip install makesure

Usage

Create a schema that defines your data and call the function make_sure(your_schema,input_data) if any validation fails it will raise an exception MakeSureException otherwise it return the data.

Schemas

A schema is a dict that defines your data.

Keys Description
required key must be in data and cannot be None
min value grater than or equal to min
max value less than or equal to max
min_len len of value less than or equal to min_len
max_len len of value less than or equal to max_len
regx value must satisfy this reguler expressions
type data types such as int, str, list, dict, tuple
enum value must be in list or tuple or set
msg your own error message

sample schema

user_schema = {
    'name':{
        'required':True,
        'type':str
    },
    'email':{
        'type':str,
        'required':True,
        'regx':Regx.email
    },
    'age':{
        'type':int,
        'min':18
    }
}

Some Useful Regx

from makesure import Regx
Regx Description
Regx.email email regx
Regx.alpha only alphabets
Regx.number only numbers
Regx.alphanum alphanumerics string

Example

# app.py
from makesure import make_sure, Regx, MakeSureException

user_schema = {
    'name':{
        'required':True,
        'type':str,
    },
    'age':{
        'type':int,
        'min':18
    },
    'email':{
        'regx':Regx.email
    }
}

data = {
    'name':'Your Name',
    'age':12,
    'email':'asdasd'
}

try:
    result = make_sure(user_schema,data)
    print(result)
except MakeSureException as e:
    print(e)

License

MIT

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

makesure-0.4.tar.gz (3.2 kB view hashes)

Uploaded Source

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