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 details)
Built Distribution
File details
Details for the file verity-0.1.tar.gz
.
File metadata
- Download URL: verity-0.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a90bb94146f937057a10c630386f631018c77b52805ca86031d5a9ff6926b4e |
|
MD5 | c599b9d97cc6ea82f1b891e3874b1876 |
|
BLAKE2b-256 | a16b6261ffeda91e4763e700cf1439038ccbc3b64f520a18f2b61e76c1205b8a |
File details
Details for the file verity-0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: verity-0.1-py2.py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0366c9f644a5838064581182e180759181ee37d287fda40400bf6a52231352d9 |
|
MD5 | e5c18174535dd6506c626a76d9b1d4d0 |
|
BLAKE2b-256 | 3cbe1f850f675102be9f8f3a4ddac90f8f5eb705fc82e5d2b74792c458faaa92 |