2025-03-26 17:27:01 +01:00

51 lines
1.6 KiB
Python

from pydantic import BaseModel, Field
# Base Schema
class BookBase(BaseModel):
title: str = Field(..., description="Book title")
author: str = Field(..., description="Book author")
description: str = Field(None, description="Book description")
published_year: int = Field(None, description="Year the book was published")
class Config:
schema_extra = {
"example": {
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"description": "A novel about the decadence of the Jazz Age.",
"published_year": 1925
}
}
# Create Schema
class BookCreate(BookBase):
isbn: str = Field(..., description="Book ISBN number")
class Config:
schema_extra = {
"example": {
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"description": "A classic novel about racial injustice.",
"published_year": 1960,
"isbn": "978-0060935467"
}
}
# Response Schema
class Book(BookBase):
id: int = Field(..., description="Book ID")
isbn: str = Field(..., description="Book ISBN number")
class Config:
orm_mode = True
schema_extra = {
"example": {
"id": 1,
"title": "1984",
"author": "George Orwell",
"description": "A dystopian novel about totalitarianism.",
"published_year": 1949,
"isbn": "978-0451524935"
}
}