Skip to content

Instantly share code, notes, and snippets.

@duonghuuphuc
Created March 6, 2025 14:44
Show Gist options
  • Save duonghuuphuc/9b0bb0bce4d5f4f4ba3e4e0b5d987ff4 to your computer and use it in GitHub Desktop.
Save duonghuuphuc/9b0bb0bce4d5f4f4ba3e4e0b5d987ff4 to your computer and use it in GitHub Desktop.
CS504070 - Homework 7: Multiple Parameter Types with Validation

CS504070 - Homework 7


Exercise 1: API with Multiple Parameter Types

Task

  1. 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.
  2. The API should:

    • Validate item_id to be a positive integer.
    • Ensure price is greater than zero.
    • Return a response containing the processed parameters.

Hints

  • Use Annotated[int, (Field/Path)] for validation.
  • Use BaseModel for request body validation.

Exercise 2: API with a List Field

Task

  1. Create a FastAPI endpoint /cart/ that accepts a request body representing a shopping cart.
  2. The request body should contain:
    • user_id (integer, required)
    • items (list of strings, min 1 item)
  3. Return the received data as a response.

Requirements

  • user_id should be a positive integer.
  • items should be a non-empty list.

Hints

  • Use Annotated for validation.
  • Use Field(min_items=1) to enforce at least one item.

Exercise 3: Nested Model for Orders

Task

  1. Extend the API by adding an /order/ endpoint that accepts nested models in the request body.
  2. The request body should have:
    • customer_id (integer, positive)
    • order_items (list of order objects)
  3. Each order object must contain:
    • product_id (integer, required)
    • quantity (integer, min 1)
    • price (float, must be positive)

Requirements

  • Use Annotated for validation.
  • Ensure order_items is a non-empty list.

Hints

  • Define OrderItem as a separate BaseModel and use it inside the main model.

Exercise 4: Nested Model with Validation & Optional Fields

Task

  1. Create a /invoice/ endpoint that accepts a complex invoice request with optional fields.
  2. The request body should include:
    • invoice_id (UUID)
    • customer_details (nested model with name, email)
    • products (list of nested models)
  3. 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)

Requirements

  • Use Optional for nullable fields. Refer to the typing.Optional of Python.
  • Use confloat to limit discount percentage. Refer to the Constrained types of Pydantic.

Hints

  • Utilize Annotated and Field for detailed validation.
  • Use UUID for invoice_id. Refer to the UUID documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment