Skip to main content

Function: validateBody()

validateBody<T>(schema): MiddlewareHandler<ChiveEnv>

Defined in: src/api/middleware/validation.ts:112

Validates request body against a Zod schema.

Type Parameters

T

Schema output type

Parameters

schema

ZodType<T>

Zod schema for validation

Returns

MiddlewareHandler<ChiveEnv>

Middleware that validates body and sets validatedInput

Remarks

Handles JSON parse errors gracefully, converting them to ValidationError.

Example

const bodySchema = z.object({
q: z.string().min(1),
facets: z.array(z.string()).optional(),
limit: z.number().min(1).max(100).default(50),
});

app.post(
'/xrpc/pub.chive.preprint.searchSubmissions',
validateBody(bodySchema),
(c) => {
const body = c.get('validatedInput') as z.infer<typeof bodySchema>;
// body.q, body.facets, body.limit are type-safe
}
);