StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 3 · Request Body and Pydantic Models

Nested Shipment Structure

easy—No data yet

Problem statement

A shipment carries one destination address and a list of parcels, but the request model flattens them into loose text fields, so neither can be validated. Replace it with nested models, so every part of the structure carries its own rules and a violation is reported at the exact place it happened.

API Endpoints to Implement

The three models live in app/models/shipment.py. A key none of them declares is ignored wherever it appears.

Address

  • line1, text, required, at least 3 characters.
  • city, text, required, at least 2 characters.
  • postal_code, text, required, matching the pattern ^[0-9]{5}$.
  • country, text, required, matching the pattern ^[A-Z]{2}$.

Parcel

  • tracking_code, text, required, matching the pattern ^P[0-9]{6}$.
  • weight_kg, a number, required, greater than 0_ and at most _30.
  • fragile, a boolean, optional, default false.

ShipmentCreate

  • reference, text, required, at least 3_ and at most _20 characters.
  • destination, one Address, required.
  • parcels, a list of Parcel, required, at least 1_ and at most _5 of them.

POST /shipments

Answers 200 with the receipt build_receipt produces from the models above. A value readable as its declared type is converted to it, so a weight_kg of 3_ or of _"3" is reported as the number 3.0.

Rejected requests

A violation anywhere is answered with 422 and one detail entry per broken rule, carrying the type and msg reported for that rule, the input that was received, and, for a length or a bound, a ctx. A missing field reports the object it was missing from as its input.

loc names the path to the offending value: a field of the address is ["body", "destination", "city"], the parcel list itself is ["body", "parcels"], a whole parcel is ["body", "parcels", 1] with its position, and a field of one parcel is ["body", "parcels", 0, "tracking_code"].

A list outside its size reports too_short or too_long, worded List should have at least 1 item after validation, not 0 and List should have at most 5 items after validation, not 6.

Sample Requests & Responses

POST /shipments

Request:

{
  "reference": "SHP-1001",
  "destination": {"line1": "12 Harbor Way", "city": "Lisbon", "postal_code": "10045", "country": "PT"},
  "parcels": [
    {"tracking_code": "P100001", "weight_kg": 2.5, "fragile": true},
    {"tracking_code": "P100002", "weight_kg": 11.25, "fragile": false}
  ]
}

Response (200 OK):

{
  "reference": "SHP-1001",
  "destination": {"line1": "12 Harbor Way", "city": "Lisbon", "postal_code": "10045", "country": "PT"},
  "parcel_count": 2,
  "total_weight_kg": 13.75,
  "fragile_count": 1,
  "parcels": [
    {"tracking_code": "P100001", "weight_kg": 2.5, "fragile": true},
    {"tracking_code": "P100002", "weight_kg": 11.25, "fragile": false}
  ]
}

POST /shipments

Request:

{
  "reference": "SHP-1001",
  "destination": {"line1": "12 Harbor Way", "city": "Lisbon", "postal_code": "ABCDE", "country": "PT"},
  "parcels": [
    {"tracking_code": "P100001", "weight_kg": 2.5, "fragile": true},
    {"tracking_code": "P100002", "weight_kg": 40, "fragile": false}
  ]
}

Response (422 Unprocessable Entity):

{
  "detail": [
    {
      "type": "string_pattern_mismatch",
      "loc": ["body", "destination", "postal_code"],
      "msg": "String should match pattern '^[0-9]{5}$'",
      "input": "ABCDE",
      "ctx": {"pattern": "^[0-9]{5}$"}
    },
    {
      "type": "less_than_equal",
      "loc": ["body", "parcels", 1, "weight_kg"],
      "msg": "Input should be less than or equal to 30",
      "input": 40,
      "ctx": {"le": 30.0}
    }
  ]
}

More in Request Body and Pydantic Models

See the module
Item Creation ModelThis problem covers describing the shape of the data a caller may send when adding an item, so the parts they leave out get sensible values and the parts they get wrong are refused by name.0%0 of 1 solvedeasyField Constrained OrderThis problem covers putting limits on each value an order carries, so a quantity, a price or a note outside what the business allows is refused before the order is ever placed.—No data yeteasyBooking Request PayloadThis problem covers taking a booking that arrives split across the address, the query and a nested body, checking every part of it, and turning down anything that does not fit before the trip is even looked up.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.