Problem statement
The book search handler declares no query parameters, so every search fails. Declare term and limit with rules on their length, characters and range, so a value that breaks a rule is rejected before the search runs.
Requirements
The app, the GET / home route, the book data in app/store.py, the find_books search in app/services/catalog.py and the body of the search_books handler are already provided. The body answers with term and limit as it used them, then the total and results that find_books found. Your task has one part, in app/api/routes/books.py: declare the two query parameters of search_books with their rules.
A query parameter can carry its own rules. Text can be given a smallest length, a largest length and a pattern it must match, and a number can be given a smallest and a largest value. FastAPI checks these rules before the handler runs and rejects a value that breaks one with a 422 validation error that names the parameter.
- term is required text. A request without it is rejected with a 422 validation error that names term as missing
- term has at least 3 characters. term=led is accepted, and a shorter one, such as term=st or an empty term=, is rejected with a 422 validation error naming term
- 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=Room 7 is accepted, and terms such as moon_river, sql;drop, star! and night-owl are rejected with a 422 validation error naming term
- 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
Sample Requests & Responses
GET /books/search?term=star&limit=3
Response (200 OK):
{"term": "star", "limit": 3, "total": 14, "results": [{"id": 1, "title": "Star Harbor", "author": "Ada Finch", "year": 1998}, {"id": 2, "title": "Star Weather", "author": "Ada Finch", "year": 2001}, {"id": 3, "title": "Starlight Ledger", "author": "Bo Cruz", "year": 2004}]}
GET /books/search?term=Room%207
Response (200 OK):
{"term": "Room 7", "limit": 10, "total": 1, "results": [{"id": 18, "title": "Amber Room 7", "author": "Omar Pike", "year": 2010}]}
GET /books/search
Response (422 Unprocessable Entity):
{"detail": [{"type": "missing", "loc": ["query", "term"], "msg": "Field required", "input": null}]}
GET /books/search?term=st
Response (422 Unprocessable Entity):
{"detail": [{"type": "string_too_short", "loc": ["query", "term"], "msg": "String should have at least 3 characters", "input": "st", "ctx": {"min_length": 3}}]}
GET /books/search?term=night-owl
Response (422 Unprocessable Entity):
{"detail": [{"type": "string_pattern_mismatch", "loc": ["query", "term"], "msg": "String should match pattern '^[A-Za-z0-9 ]+$'", "input": "night-owl", "ctx": {"pattern": "^[A-Za-z0-9 ]+$"}}]}
GET /books/search?term=star&limit=100000
Response (422 Unprocessable Entity):
{"detail": [{"type": "less_than_equal", "loc": ["query", "limit"], "msg": "Input should be less than or equal to 50", "input": "100000", "ctx": {"le": 50}}]}
