Skip to content

Latest commit

 

History

History
56 lines (48 loc) · 1.21 KB

README.md

File metadata and controls

56 lines (48 loc) · 1.21 KB

pydantic-jsonapi

Build Status PyPi Link

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)