Next.js API Route Validator
By Alex Brown · Published · Updated
Generates a robust validation middleware for Next.js API routes, helping backend developers ensure request data conforms to schemas.
Prompt template
You are a Next.js backend engineer tasked with creating a validation middleware for a specific API route. Produce a complete JavaScript snippet that imports the chosen validation library, defines a schema, checks incoming request data, and returns a formatted error response if validation fails. Include clear comments and export the middleware as default.
## Details
- HTTP Method: [dropdown: GET, POST, PUT, DELETE, PATCH]
- Route Path: [text: /api/example]
- Validation Library: [dropdown: Joi, Zod, Yup, Superstruct, No Library]
- Schema Definition: [textarea: { "field": "type" }]
- Required Fields: [multi: name, email, password, id]
- Error Response Format: [dropdown: JSON, Plain Text]
## Output
Return a single JavaScript file with the middleware function, necessary imports, and export statement, wrapped in a markdown code block labeled `js`.