Skip to content

Simple way to validate/cast incoming JSON data.

License

Notifications You must be signed in to change notification settings

funkybob/verity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verity

Verity provides Type and field as ways to build classes which will cast and validate your incoming data.

Simply declare a sub-class of Type, with fields. Each field takes a callable, which is used to validate/cast the incoming data of its name. It can optionally also take a default value.

QuickStart

from datetime import datetime
from verity import Type, field

def parse_date(value):
    return datetime.strptime(value, '%Y-%m-%d').date()

class Person(Type):
    name = field(str)
    birthdate = field(parse_date)
>>> data = {'name': 'Bob', 'birthdate': '1980-12-21'}
>>> person = Person(data)
>>> person.birthdate
datetime.date(1980, 12, 21)

Types are nestable:

class Food(Type):
    name = field(str)
    energy = field(float, default=0.0)


class Person(Type):
    name = field(str)
    birthdate = field(parse_date)
    favourite_food = field(Food)
>>> data = {'name': 'Bob', 'birthdate': '1980-12-21', 'favourite_food': {'name': 'Pizza'}}
>>> person = Person(**data)
>>> person.favourite_food.name
'Pizza'

Types can JSON-ify themselves

>>> person.__json__()
{'name': 'Bob', 'birthdate': datetime.date(1980, 12, 21), 'favourite_food': <Food>}

Though it's not recurive.

However, it can cooperate with json_default:

>>> from verity import json
>>> json.dumps(person)
'{"birthdate": "1980-12-21", "favourite_food": {"energy": 0.0, "name": "Pizza"}, "name": "Bob"}'

About

Simple way to validate/cast incoming JSON data.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages