Skip to content

commandBackend and APIs

Add Validation

Adds request validation to a handler using the validation library your project already uses, with tests for bad input.

npx workspacetemplates add add-validation --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/add-validation.md16 lines · 1.1 KBFile
---
description: Adds request validation to a handler using the validation library your project already uses, with tests for bad input.
argument-hint: "[handler file or route]"
---

Add request validation to: $ARGUMENTS

1. Read the handler and list every value it takes from the body, query, params and headers, with the type and limits the code assumes.
2. Find the validation library already in the dependencies and copy how an already validated handler uses it (where the schema lives, how errors are mapped). Never add a second library.
3. Write the schema: required versus optional, string length caps, numeric ranges, enums, email or UUID formats, and reject unknown keys when the project does so elsewhere.
4. Run it before any logic, and return the project's standard 400 shape with the field name in each message.
5. Use the parsed value afterwards, not the raw request.
6. Add tests: valid input, each missing required field, wrong types, boundary values, and an extra unknown field.
7. Run tests and lint.

Report each rule you added and why, and mark any limit you had to guess so I can confirm it. Do not change what valid requests return.

What it does

Handlers that trust the request body fail in odd places, far from the real cause. This command puts a schema at the edge of the handler so bad input is rejected early with a clear message.

It does not bring in a new library. It finds whether you use Zod, Joi, Pydantic, class-validator or something else, and follows the way existing handlers use it.

Example

you › /add-validation src/api/signup.ts

agent › Schema added: email (format, max 254), password (12-128), name (1-80, trimmed), plan enum free|pro.
Invalid input now returns 400 {errors:[{field,message}]}. 9 new tests pass.
Guessed: name max 80, please confirm.

Questions

What if there is no validation library yet?
It tells you so and proposes one that suits your stack, then waits for your decision before installing anything.
Does it change responses for valid calls?
No. Only invalid requests behave differently, and the tests prove valid ones still pass.
  • Add Caching

    CommandBackend and APIs

    Free
  • Add Endpoint

    CommandBackend and APIs

    Free
  • Add Health Check

    CommandBackend and APIs

    Free
  • Add Pagination

    CommandBackend and APIs

    Free