Skip to main content

A simple, customizable, and generic parameter validation api.

Project description

## API Params

This projoct is designed to provide a simple solution to parameter parsing in restful api services.
There are a lot of solutions for parameter parsing out there, but I couldn't find one that did what I wanted so I built this.

Basically the idea is that you create a schema where you explicity define all the fields which your api endpoint should provide. You can set fields as either required or not and you can decide to attempt to coerce input values if you want to, for instance you could coerce a '1' to a boolean True in python or the string '14.38' into the float 14.38, etc.

Next for each field you can define validator functions which take the (possibly coerced) input value and return a boolean and a message indicating True for when the value successfully passes the validation and False for when the value fails. The message object is there to give the user control of how he wants to format error response messages.

If all the required parameters, coercions and validator functions work then a tuple object that is returned in which the first element is a boolean True for success and the second element is the parsed and coerced hash of parameter values. On failure a False value is returned as well as a dictionary of parameter name to list of error message mappings is returned indicating the reason for failure.

#### Usage Example

from api_params.schema import Schema, Validator
from api_params.types import Type

# Create the schema
schema = Schema()
schema.field('username', Type.STRING, required=True)
schema.field('password', Type.STRING, required=True)
schema.field('remember_me', Type.BOOLEAN, required=True, coerce=True)
schema.field('selected_number', Type.NUMBER, required=True, coerce=True)

# write some validation functions
# all validation functions must return a tuple (pass_or_fail, message)
def not_empty(value):
return (len(value) > 0, "cannot be empty")

def create_in_range(min_value, max_value):
def in_range(value):
return (value >= min_value and value <= max_value, "value must be >= {} and <= to {}".format(min_value, max_value))
return in_range

# Create the validator
validator = Validator(schema)
validator.add_validator('username', not_empty)
validator.add_validator('password', not_empty)
validator.add_validator('selected_number', create_in_range(0,100))

# Now run the validations on some input
success, parsed_result = validator.validate({
'username' : 'bob',
'password' : '123456',
'selected_number' : '85',
'remember_me' : '1'

print(success) # True
print(parsed_result) # { 'username' : 'bob', 'password' : '123456', 'remember_me' : True, 'selected_number' : 85 }

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for api-params, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size api_params-0.0.2.tar.gz (4.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page