an implementation of JSON:api using pydantic for validation
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)