Skip to content

an implementation of JSON:api using pydantic for validation

License

Notifications You must be signed in to change notification settings

DeanWay/pydantic-jsonapi

Repository files navigation

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)

About

an implementation of JSON:api using pydantic for validation

Resources

License

Stars

Watchers

Forks

Packages

No packages published