Problem statement
The catalog search handler declares no query parameters, so every request fails. Declare all six so that the hyphenated names are read, repeated tags are collected into a list, the optional filters fall back to their defaults and every rule on length, characters and range is enforced.
Requirements
The app, the GET / home route, the listing data in app/store.py, the search_catalog search in app/services/search.py and the body of the search_listings handler are already provided. The body answers with resolved, the six values the search ran with under the keys region_id, tags, term, in_stock, limit and offset, then the total and results that search_catalog found. Your task has one part, in app/api/routes/catalog.py: declare the six query parameters of search_listings, named region_id, tag, term, in_stock, limit and offset, so that all of the rules below hold at the same time.
This question uses everything from the module: required and optional query parameters with their types and defaults, rules on length, pattern and range, repeated parameters and aliases.
- region_id is a required integer, read from the public name region-id. A request without it is rejected with a 422 validation error that names region-id as missing, region-id=2 arrives as the number 2, and a value that is not a whole number, such as region-id=north, is rejected with a 422 validation error naming region-id
- region-id is the only name read for the region. A value sent as region_id, with an underscore, is ignored
- tag is optional text that can be repeated up to 3 times. Every occurrence is collected into a list in the order it arrived, with repeats kept, so tag=seasonal arrives as ["seasonal"] and tag=featured&tag=seasonal&tag=featured arrives as ["featured", "seasonal", "featured"]. A fourth occurrence is rejected with a 422 validation error naming tag, and a request without tag uses the list ["catalog"]
- term is optional text that defaults to null
- term has at least 3 characters. term=lam is accepted, and a shorter one, such as term=la or an empty term=, is rejected with a 422 validation error naming term. Sending an empty term is not the same as leaving it out
- term has at most 20 characters. A term of 20 characters is accepted, and a term of 21 characters is rejected with a 422 validation error naming term
- term holds only English letters, digits and spaces, which is the pattern ^[A-Za-z0-9 ]+$. term=Desk Lamp is accepted, and terms such as desk-lamp, wall_art, lamp! and sql;drop are rejected with a 422 validation error naming term
- in_stock is an optional boolean that defaults to false, read from the public name in-stock only. It reads true, 1, yes, y, t and on as true and false, 0, no, n, f and off as false, in any letter case. Any other value, such as maybe, is rejected with a 422 validation error naming in-stock, and a value sent as in_stock, with an underscore, is ignored
- limit is an optional whole number that defaults to 10. limit=3 arrives as the number 3
- limit is at least 1. limit=1 is accepted, and limit=0 or limit=-3 is rejected with a 422 validation error naming limit
- limit is at most 50. limit=50 is accepted, and limit=51 or limit=100000 is rejected with a 422 validation error naming limit
- offset is an optional whole number of at least 0 that defaults to 0. offset=3 arrives as the number 3, offset=0 is accepted, and offset=-1 or offset=-5 is rejected with a 422 validation error naming offset
Sample Requests & Responses
GET /catalog/search?region-id=9
Response (200 OK):
{"resolved": {"region_id": 9, "tags": ["catalog"], "term": null, "in_stock": false, "limit": 10, "offset": 0}, "total": 0, "results": []}
GET /catalog/search?region-id=1&tag=seasonal&tag=featured&term=lam&in-stock=on&limit=2&offset=1
Response (200 OK):
{"resolved": {"region_id": 1, "tags": ["seasonal", "featured"], "term": "lam", "in_stock": true, "limit": 2, "offset": 1}, "total": 2, "results": [{"id": 12, "name": "Lagoon Table Lamp", "price": 47.0, "stock": 9, "region_id": 1, "tags": ["catalog", "featured"]}]}
GET /catalog/search?region_id=1
Response (422 Unprocessable Entity):
{"detail": [{"type": "missing", "loc": ["query", "region-id"], "msg": "Field required", "input": null}]}
GET /catalog/search?region-id=1&tag=catalog&tag=featured&tag=clearance&tag=imported
Response (422 Unprocessable Entity):
{"detail": [{"type": "too_long", "loc": ["query", "tag"], "msg": "List should have at most 3 items after validation, not 4", "input": ["catalog", "featured", "clearance", "imported"], "ctx": {"field_type": "List", "max_length": 3, "actual_length": 4}}]}
GET /catalog/search?region-id=1&term=desk-lamp
Response (422 Unprocessable Entity):
{"detail": [{"type": "string_pattern_mismatch", "loc": ["query", "term"], "msg": "String should match pattern '^[A-Za-z0-9 ]+$'", "input": "desk-lamp", "ctx": {"pattern": "^[A-Za-z0-9 ]+$"}}]}
