# Add Input Validation to a Hono API

Yes. An agent adds Zod schemas and the Hono validator to your write routes so bad JSON gets a 400 with field errors, extends the tests and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-input-validation/hono/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how a request reaches a handler today. In a Worker like the `blog` workspace of honojs/examples, `package.json` depends on `hono` with a caret on 4.2.4 and tests with `vitest` through `@cloudflare/vitest-pool-workers`, whose `vitest.config.ts` points at `wrangler.toml`; the root `package.json` lists the workspaces with `npm@10.0.0` and no lockfile. `src/index.ts` mounts `api` under `/api` behind a middleware app that runs `prettyJSON` and `basicAuth` for every non-GET on `/posts/*`, with the credentials read from the `USERNAME` and `PASSWORD` bindings in `src/bindings.ts`. `src/api.ts` has `POST /posts` and `PUT /posts/:id` calling `c.req.json()` and casting the result `as model.Param` with no check at all.

What catches bad input now is `src/model.ts`: `createPost` returns nothing unless `param.title` and `param.body` are truthy, which `api.ts` turns into a `422` with `Can not create new post`, and `updatePost` returns `false` the same way, which becomes `{ ok: false }` with a `200`. A body that is not JSON throws before either check and ends as a `500`. A client learns nothing about which field was wrong, an empty string and a missing field are treated alike, and a number in `title` is stored as is.

The agent adds `zod` and `@hono/zod-validator` to the workspace, defines a `postSchema` in a new `src/schemas.ts` with `title` and `body` as non-empty trimmed strings, and applies `zValidator('json', postSchema)` to `POST /posts` and `PUT /posts/:id`, reading the parsed body with `c.req.valid('json')` so the `as model.Param` casts go away. The validator's hook answers a `400` with `{ ok: false, errors: [...] }` listing each failing path and message, in the same `ok` shape the other responses use; a body that is not JSON gets the same `400`. The `id` parameter on `GET`, `PUT` and `DELETE /posts/:id` gets a `zValidator('param', ...)` that requires a non-empty string, because `model.ts` prefixes it into a KV key.

Tests extend `test/api.test.ts`, which already calls `worker.request` with `env` from `cloudflare:test` and walks a post through create, read, update and delete: the agent adds cases for a missing `body`, an empty `title`, a numeric `title`, and a request whose body is not JSON, each expecting `400` with the field named in `errors`, and keeps the existing `422` case meaningful by asserting the happy path still returns `201`. It copies `wrangler.example.toml` to `wrangler.toml`, since the test pool reads it, runs `npm -w blog test`, which is `vitest --run`, runs `npx tsc --noEmit` against the workspace `tsconfig.json`, starts `npm -w blog run dev` and sends a few `curl` requests with bad bodies to keep the real answers.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "codex",
  "input": "Add input validation to the blog workspace of this repository and open a pull request. Read the root package.json, blog/package.json, blog/src/index.ts, blog/src/api.ts, blog/src/model.ts, blog/src/bindings.ts, blog/test/api.test.ts, blog/vitest.config.ts, blog/wrangler.example.toml and blog/tsconfig.json first. Add zod and @hono/zod-validator to blog/package.json. Create blog/src/schemas.ts with a post schema whose title and body are non-empty trimmed strings and an id parameter schema. Apply zValidator for json on POST /posts and PUT /posts/:id and for param on every /posts/:id route in api.ts, read the parsed values with c.req.valid, and remove the as model.Param casts. Make a failed validation answer 400 with ok false and an errors array naming each path and message, and make a non-JSON body answer the same 400. Extend blog/test/api.test.ts with cases for a missing body field, an empty title, a numeric title and a non-JSON body, and keep the existing create, read, update and delete tests passing. Run npm install at the root, copy blog/wrangler.example.toml to blog/wrangler.toml, run npm -w blog test and npx tsc --noEmit in blog, then start npm -w blog run dev and send curl requests with a valid and an invalid body. Paste the test and curl output in the pull request. Do not change model.ts, index.ts or any other workspace.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request confined to the `blog` workspace: `zod` and `@hono/zod-validator` in its `package.json`, a new `src/schemas.ts`, `zValidator` calls on the write and `:id` routes in `src/api.ts` with the casts removed, and new cases in `test/api.test.ts`. The description pastes the Vitest run, the `tsc` result, and `curl` output showing a `201` for a good body and a `400` with the `errors` array for a bad one.

A reviewer checks that the `errors` array names the field and not just a generic message, that `model.ts` is untouched and still guards KV writes on its own, that `basicAuth` in `index.ts` still runs before the validator on non-GET requests so an unauthenticated client cannot probe the schema, and that the `422` branch in `api.ts` is now unreachable through HTTP but kept as a last line of defence.

The repository has no workflow that runs the examples, so the pasted test run and `curl` session are what the reviewer reads before merging.

## Run it on every new write endpoint

A team adding routes to several Hono services starts one run per repository with `POST /o/{org}/sessions`, this prompt with the workspace and routes swapped in, and the repository in `resources`. The agent reads each service's own handlers and model, so a route that takes form data gets `zValidator('form', ...)` and one that reads query parameters gets `'query'`, and each run opens its own pull request with its own test output.

The key's `webhook_url` receives `run.completed` for each run it starts, with `data.session_url` for the pull request link.

## Why run it on Rush?

Validation added by hand tends to cover the fields someone remembered and the error shape someone chose that day. In a Rush sandbox the agent reads every route that touches a request body, writes one schema the routes share, runs the Workers test pool and the TypeScript compiler, and sends real bad bodies to the dev server, so the pull request shows the exact `400` a client will get.

The same request works across every Hono service the team owns, each run in its own sandbox with the test output kept, and a reviewer reads one schema file instead of chasing casts through handlers.

## Frequently asked questions

### Why Zod and not Hono's built-in validator alone?

`hono/validator` takes any function, but a Zod schema gives typed `c.req.valid('json')` results, field-level messages for free, and a single place to change a rule. `@hono/zod-validator` is the thin adapter between the two, and the `pages-stack` example in the same repository already pairs them.

### Does model.ts still check title and body?

Yes. The agent leaves `createPost` and `updatePost` as they are, so a caller that bypasses the API and uses the model directly still gets the same guard. The validator makes those branches unreachable through HTTP and gives the client a better answer.

### What does a client see on a bad request?

A `400` with `{ ok: false, errors: [...] }`, where each entry names the path such as `body` or `title` and the message from the schema. A body that is not JSON gets the same status and shape rather than a `500`.

### Do the tests need a Cloudflare account?

No. `vitest.config.ts` uses `@cloudflare/vitest-pool-workers`, which runs the Worker and its KV binding locally from `wrangler.toml`, the same way the existing create, read, update and delete tests already run.

## More Hono jobs

- [Add JWT Auth to a Hono API from a Linear Issue](https://getrush.ai/run/add-jwt-auth/hono/)
- [Add OpenAPI Docs to a Hono API](https://getrush.ai/run/add-openapi-docs/hono/)
- [Add Rate Limiting to a Hono API from a Linear Issue](https://getrush.ai/run/add-rate-limiting/hono/)

## The same job on other stacks

- [Add Input Validation to a Koa App](https://getrush.ai/run/add-input-validation/koa/)
- [Add Input Validation to a Ktor API](https://getrush.ai/run/add-input-validation/ktor/)
- [Add Input Validation to a Node.js Express App](https://getrush.ai/run/add-input-validation/nodejs/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
