Skip to content

DeanWay/pydantic-jsonapi

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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