Skip to content

Instantly share code, notes, and snippets.

@swilcox
Created May 12, 2025 22:44
Show Gist options
  • Save swilcox/a89d7ad3273631866589096bfbf21f9c to your computer and use it in GitHub Desktop.
Save swilcox/a89d7ad3273631866589096bfbf21f9c to your computer and use it in GitHub Desktop.
litestar pydantic post endpoint model validation?
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