-
Notifications
You must be signed in to change notification settings - Fork 2
Validation
Nadhi-(Kushi) edited this page Apr 1, 2026
·
2 revisions
@http-native/core/validate is schema-agnostic middleware for request validation.
Supported schema styles in the current implementation:
.safeParse().parse().validate()
That covers common Zod, TypeBox, and Joi flows.
import { createApp } from "@http-native/core";
import { validate } from "@http-native/core/validate";
import { z } from "zod";
const app = createApp();
const createUserSchema = z.object({
name: z.string().min(1),
email: z.string().email(),
});
app.post("/users", validate({ body: createUserSchema }), (req, res) => {
res.status(201).json(req.validatedBody);
});The middleware writes parsed values onto the request:
req.validatedParamsreq.validatedQueryreq.validatedBody
const searchSchema = z.object({
q: z.string().min(1),
page: z.coerce.number().int().min(1).default(1),
});
app.get("/search", validate({ query: searchSchema }), (req, res) => {
res.json(req.validatedQuery);
});const paramsSchema = z.object({
id: z.coerce.number().int().positive(),
});
app.get("/users/:id", validate({ params: paramsSchema }), (req, res) => {
res.json(req.validatedParams);
});Validation failures return 400 JSON responses. The current middleware uses:
{
"error": "Validation Error",
"field": "body",
"details": []
}Depending on schema library, details can be a string or an array of { path, message } objects.
Http-native is open to everyone, but we have strict regulations and licensing fees for large organizations