Skip to content

Body — Fields

Use Field() to add metadata and constraints to Pydantic model fields:

from pydantic import BaseModel, Field
from typing import Optional
from justapi import JustAPIApp
class Item(BaseModel):
name: str = Field(..., min_length=1, max_length=100)
price: float = Field(..., gt=0)
description: Optional[str] = Field(None, max_length=500)
tags: list[str] = Field(default_factory=list)
app = JustAPIApp()
@app.post("/items")
def create_item(item: Item):
return item.model_dump()
Option Type Description
default Any Default value
alias str Alternative field name
title str Title in OpenAPI docs
description str Description in OpenAPI docs
examples list Example values
min_length int Minimum string length
max_length int Maximum string length
gt Number Greater than
ge Number Greater than or equal
lt Number Less than
le Number Less than or equal
pattern str Regex pattern
class CreateUser(BaseModel):
username: str = Field(
...,
min_length=3,
max_length=50,
pattern=r"^[a-zA-Z0-9_]+$",
title="Username",
description="Alphanumeric characters and underscores only",
examples=["alice_123"],
)
email: str = Field(
...,
pattern=r"^[\w.-]+@[\w.-]+\.\w+$",
title="Email address",
)
age: int = Field(
...,
ge=13,
le=120,
title="Age",
description="Must be between 13 and 120",
)