StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 2 · Query Parameters

Constrained Query Rules

easy100%1 of 1 solved

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

More in Query Parameters

See the module
Optional Search FiltersThis problem covers searching a store's product catalog with filters a caller may leave out, so an omitted filter falls back to a sensible default and a value that makes no sense is refused with a clear reason.100%1 of 1 solvedeasyRepeated And Aliased ParametersThis problem covers reading search options a caller may send more than once or spell with a hyphen, so repeating a filter narrows the listing by each value in turn instead of only the last.100%1 of 1 solvedeasyCatalog Search ContractThis problem covers accepting a full set of search options from a caller, filling in the ones left out, refusing the ones that break a rule, and reporting back exactly which options the search ran with.100%1 of 1 solvedeasy

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.