Files
What you'll learn
- Put validator('json', …) from hono/validator in front of a handler and answer 400 from inside it
- Read the validated, typed value with c.req.valid('json') instead of parsing the body again
- Validate a path parameter and a query value the same way, and share a validator between routes
Press Run tests to start the app. Its log appears here.Graded endpoints
The validator accepts the body and the handler stores the cat
The validator refuses an empty name with a message naming the field; the handler never runs
An age that is not a non-negative integer is refused the same way
The validator returns only the fields it checked, so an extra field never reaches the stored cat
No limit in the query string means every cat
The query validator converts the string to a number the handler can slice with
A limit outside 1 to 100 is a 400 from the query validator
The param validator accepts digits and hands the handler a number
Refused by the param validator before any lookup: 400, not 404
The same param validator, shared with the DELETE route
A valid id that no cat has: past the validator, the handler's own 404
A valid id reaches the handler, which removes the cat
The id is valid but the cat is gone: the handler's 404