Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/DeanWay/pydantic-jsonapi

an implementation of JSON:api using pydantic for validation
https://github.com/DeanWay/pydantic-jsonapi

Last synced: 13 days ago
JSON representation

an implementation of JSON:api using pydantic for validation

Awesome Lists containing this project

README

        

# pydantic-jsonapi
[![Build Status](https://travis-ci.org/DeanWay/pydantic-jsonapi.svg?branch=master)](https://travis-ci.org/DeanWay/pydantic-jsonapi)
[![PyPi Link](https://img.shields.io/pypi/pyversions/pydantic-jsonapi.svg)](https://pypi.org/project/pydantic-jsonapi/)

an implementation of JSON:api using pydantic for validation

```python
from pydantic_jsonapi import JsonApiModel
from pydantic import BaseModel

class Item(BaseModel):
name: str
quantity: int
price: float

ItemRequest, ItemResponse = JsonApiModel('item', Item)

# request validation
request = {
'data': {
'type': 'item',
'attributes': {
'name': 'apple',
'quantity': 10,
'price': 1.20,
},
}
}
ItemRequest(**request)

#response validation
response = {
'data': {
'id': 'abc123',
'type': 'item',
'attributes': {
'name': 'apple',
'quantity': 10,
'price': 1.20,
},
'relationships': {
'store': {
'links': {
'related': '/stores/123'
}
}
}
},
'links': {
'self': '/item/abc123'
}
}
ItemResponse(**response)
```