Created
May 12, 2025 22:44
-
-
Save swilcox/a89d7ad3273631866589096bfbf21f9c to your computer and use it in GitHub Desktop.
litestar pydantic post endpoint model validation?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Annotated | |
from pydantic import BaseModel, Field | |
from litestar import Litestar, get, post | |
class Book(BaseModel): | |
id: Annotated[int, Field(gt=0, lt=10000)] | |
title: str | |
author: str | |
@get("/") | |
async def index() -> str: | |
return "hello, world" | |
@get("/books/{book_id:int}") | |
async def get_book(book_id: int) -> Book: | |
return Book(id=book_id, title="My Book", author="S.C. Wilcox") | |
@post("/books") | |
async def post_book(data: Book) -> Book: | |
return data | |
app = Litestar([index, get_book, post_book]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment