Quick and dirty jsonapi response validator
Project description
Quick and dirty jsonapi response validator
A library for validating jsonapi responses and simplifying their handling.
What it does:
- Validates that types have the fields you want, of the type you want.
- Links resources together.
- Validates that resources you wanted included, are included.
- Pulls up relationships and attributes one level up, to resource object level.
What it doesn't do:
- Validate that response is valid jsonapi. Use jsonapi's jsonschema.
- Build requests or handle http codes.
- ORM.
Usage
Here's example usage:
# Start by defining your types
from qdjarv import Parser, Type, Rel
# Field value can be any callable that either returns a validated value or
# throws.
# 'Type' just validates that the value is of a given type.
types = {
"articles": {
"title": Type(str),
"author": Rel("people"), # One-to-one relation.
"comments": Rel(["comments"]), # One-to-many relation.
},
"people": {
"firstName": Type(str),
"lastName": Type(str),
"twitter": Type(str),
},
"comments": {
"body": Type(str),
"author": Rel("people")
}
}
# If you're using sparse fields, define those, too.
fields = {
"articles": ["title", "author"],
"people": ["firstName", "lastName"]
}
# If you're using includes, also define them.
# This is equivalent to 'author,author.comments'.
include = {
"author": {
"comments": {}
}
}
# Also declare your toplevel type.
top = ["articles"] # This is a list, single element would be just "articles".
# Finally, create a parser.
p = Parser(top, types, include=include, fields=fields)
# Parsing modifies the received message, so make a copy if you want the
# original!
# Also remember to pass the message through jsonapi jsonschema first.
# If something goes wrong, it will throw a qdjarv.ValidationError.
parsed = p.parse(message)
# If you don't feel like repeating yourself, you can get your get parameters
# like so:
fields_args = p.fields_args()
include_args = p.include_args()
Here's an example parsed message:
{
# Other toplevel stuff skipped for brevity.
"data": [
{
"type": "articles",
"id": "1",
# Relationships and attributes were pulled out to this level.
# You might want to access links and meta (and maybe
# relationships / attributes), so they're kept with a dot
# prepended.
".links": {
},
".meta": { # ...
}
".relationships": { # ...
}
".attributes": { # ...
},
"title": "JSON:API paints my bikeshed!"
"author": {
"links": {
# ...
},
"data": {
# If this resource was included, we replaced the binding
# with the object itself.
# Watch out for loops!
"type": "people",
"id": "9",
".attributes": { # ...
},
".links": { # ...
},
"firstName": "Dan",
"lastName": "Gebhardt",
"twitter": "dgeb"
}
},
"comments": { # ...
},
}
],
"included": [
# Included objects are still here, in case you wanted them. Also linked
# and flattened.
{
"type": "people",
"id": "9",
# ...
},
# ...
]
}
TODO
- Tests.
- Turning types into requests, maybe.
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
qdjarv-0.1.tar.gz
(4.4 kB
view details)
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
qdjarv-0.1-py3.7.egg
(7.6 kB
view details)
qdjarv-0.1-py3-none-any.whl
(5.1 kB
view details)
File details
Details for the file qdjarv-0.1.tar.gz.
File metadata
- Download URL: qdjarv-0.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ed047ea7812aad53ef3104976834a6e2a0fcdf2b6241cd4573a13a3fc99ac90
|
|
| MD5 |
570f78e96228f77e6aee449b8c4b253d
|
|
| BLAKE2b-256 |
c472f02467292f699564e575cf8e742438a9f0775303ce0c8a0fdc07199d7435
|
File details
Details for the file qdjarv-0.1-py3.7.egg.
File metadata
- Download URL: qdjarv-0.1-py3.7.egg
- Upload date:
- Size: 7.6 kB
- Tags: Egg
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ab62890b9b527a990983170b709fc5b3010ef2c2931394157cb764412c15171
|
|
| MD5 |
36966a13100a5d01567c12bb02236d24
|
|
| BLAKE2b-256 |
2df91c041175975f3daf53f4b9027346e0d8c794a8060e92148b5a8462616818
|
File details
Details for the file qdjarv-0.1-py3-none-any.whl.
File metadata
- Download URL: qdjarv-0.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f06fcea45707f8e108b98936dd70f6ea98e79a1563f4fa49c86ed6514eb46d1
|
|
| MD5 |
11dc4805b5c6122f41c9ff4fa254485a
|
|
| BLAKE2b-256 |
6470026141e6a0995c7e00170724091f993939a9f565508179e08739f4690451
|