Simple way to validate/cast incoming JSON data.
Project description
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)
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', 'energy': '1200'}}
>>> 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": 1200.0, "name": "Pizza"}, "name": "Bob"}'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
verity-0.1.tar.gz
(3.4 kB
view hashes)