---
name: nextjs-api-route-validator
description: "Produces a drop-in validation middleware for one Next.js API route using Joi, Zod, Yup, Superstruct, or plain checks, so bad requests get a consistent 400 before the handler ever runs. Use when a Next.js API route needs a reusable request validation middleware with Joi, Zod, Yup, Superstruct, or hand-written checks that answers 400 consistently. Do not use for generating the whole route handler or validating responses. Related topics: coding, next.js."
---

# Next.js API Route Validator

Produces a drop-in validation middleware for one Next.js API route using Joi, Zod, Yup, Superstruct, or plain checks, so bad requests get a consistent 400 before the handler ever runs.

## When to use

Use this skill when a Next.js API route needs a reusable request validation middleware with Joi, Zod, Yup, Superstruct, or hand-written checks that answers 400 consistently. Do not use it for generating the whole route handler or validating responses.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Pick the request part to validate from the method: body for POST, PUT, and PATCH, query for GET and DELETE.
3. Translate the schema into the chosen library with the selected fields required, or into explicit checks that collect every failure when no library is used.
4. Write the wrapper that parses, answers 400 in the chosen format with one entry per failed field, and attaches the parsed value as req.validated.
5. Add the usage example showing a handler wrapped with the middleware and the default export, then output the js file.

## Inputs

- **HTTP method** (choose one: GET, POST, PUT, DELETE, PATCH)
- **Route path** (free text, e.g. "/api/example")
- **Validation library** (choose one: Joi, Zod, Yup, Superstruct, No Library)
- **Schema definition** (longer free text)
- **Required fields** (choose any: name, email, password, id)
- **Error response format** (choose one: JSON, Plain Text)

## Output

One code block tagged js: imports, the schema, the validation middleware (handler in, handler out), a short usage example, and a default export.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Next.js API Route Validator

Write a request validation middleware for this Next.js API route.

- HTTP method: {{HTTP method}}
- Route path: {{Route path}}
- Validation library: {{Validation library}}
- Schema definition: {{Schema definition}}
- Required fields: {{Required fields}}
- Error response format: {{Error response format}}

Validate the right part of the request for the method: the body for POST, PUT, and PATCH, the query for GET and DELETE, since a GET body is always empty. Translate the schema definition into the chosen library's schema, marking the selected fields required and everything else optional, and use the library's own error output to build the message. With No Library, write explicit presence and type checks and collect every failure rather than stopping at the first. Export the middleware as a function that takes the handler and returns a new handler: it parses, and on failure responds 400 in the chosen format with one entry per failed field (path and reason), and on success attaches the parsed and typed value to the request as req.validated and calls the handler. Strip unknown fields rather than rejecting them, unless the schema says otherwise, and say so in a comment. Include a four-line usage example at the bottom showing the handler wrapped with the middleware. Return a single JavaScript file as a code block tagged js with imports, the schema, the middleware, the usage example, and a default export.

## Pitfalls

- Validating req.body on a GET route, where the body is always empty and the real input lives in req.query.
- Returning only the first validation error, so the client fixes fields one round trip at a time.
- Passing the raw, unparsed body to the handler after validation, which loses the library's coercions and defaults.
