Validation Configuration
Validation behavior is controlled by the validation option in the Burger constructor.
validation
src/index.ts
import { Burger } from "burger-api";
const app = new Burger({
apiDir: "./src/api",
validation: {
// Turn on automatic type conversion app-wide (default: false)
coerce: true,
// How to handle response validation: "off" | "dev" | "enforce" (default: "dev")
responseValidation: "dev",
// Error body shape: "plain" | "problem+json" (default: "problem+json")
errorFormat: "problem+json",
// Fully custom error body (optional)
errorRenderer: (result, { slot, status }) => {
return Response.json(
{ ok: false, issues: result.issues },
{ status }
);
},
},
});
| Option | Values | Default | What it does |
|---|---|---|---|
coerce | boolean | false | Enable automatic type conversion for query/params/headers/cookies. |
responseValidation | "off" | "dev" | "enforce" | "dev" | Observe-only in dev; enforce returns a safe error on mismatch. |
errorFormat | "plain" | "problem+json" | "problem+json" | The shape of the error body. |
status | number | 422 | Status code used for request validation failures. |
errorRenderer | (result, { slot, status }) => Response | None | Override the error body completely. |
Reusing schemas across routes
To share a shape between routes, define it once in its own file and import it:
src/schemas.ts
import { z } from "zod";
export const Pagination = z.object({
page: z.coerce.number().int().min(1).default(1),
limit: z.coerce.number().int().min(1).max(100).default(20),
});
api/items/schema.ts
import { Pagination } from "../../schemas";
export const GET = { query: Pagination };