-
Create a FastAPI application with an endpoint
/items/{item_id}
that:- Takes
item_id
as a path parameter (integer). - Accepts
category
as a query parameter (string, optional). - Accepts
price
as a request body parameter (float). - Uses
Annotated
for validation.
- Takes
-
The API should:
- Validate
item_id
to be a positive integer. - Ensure
price
is greater than zero. - Return a response containing the processed parameters.
- Validate
- Use
Annotated[int, (Field/Path)]
for validation. - Use
BaseModel
for request body validation.
- Create a FastAPI endpoint
/cart/
that accepts a request body representing a shopping cart. - The request body should contain:
user_id
(integer, required)items
(list of strings, min 1 item)
- Return the received data as a response.
user_id
should be a positive integer.items
should be a non-empty list.
- Use
Annotated
for validation. - Use
Field(min_items=1)
to enforce at least one item.
- Extend the API by adding an
/order/
endpoint that accepts nested models in the request body. - The request body should have:
customer_id
(integer, positive)order_items
(list of order objects)
- Each order object must contain:
product_id
(integer, required)quantity
(integer, min 1)price
(float, must be positive)
- Use
Annotated
for validation. - Ensure
order_items
is a non-empty list.
- Define
OrderItem
as a separateBaseModel
and use it inside the main model.
- Create a
/invoice/
endpoint that accepts a complex invoice request with optional fields. - The request body should include:
invoice_id
(UUID)customer_details
(nested model withname
,email
)products
(list of nested models)
- Each product object must have:
product_id
(integer, required)name
(string, min 3 characters)price
(float, must be positive)discount
(optional, float, between 0-50%)tags
(optional, list of strings)
- Use
Optional
for nullable fields. Refer to thetyping.Optional
of Python. - Use
confloat
to limit discount percentage. Refer to the Constrained types of Pydantic.
- Utilize
Annotated
andField
for detailed validation. - Use
UUID
forinvoice_id
. Refer to the UUID documentation.