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}
}
]
}
