@r01al/express-body-validation
v1.0.1
Published
Lightweight Express middleware for validating `req.body` against a schema.
Readme
express-body-validation ✅
Lightweight Express middleware for validating req.body against a schema.
Install 📦
npm install @r01al/express-body-validationUsage 🚀
import express from "express";
import { SchemaType, validateBody } from "@r01al/express-body-validation";
const app = express();
app.use(express.json());
app.post(
"/users",
validateBody({
name: SchemaType.String,
age: SchemaType.Number,
address: {
city: SchemaType.String
},
tags: [SchemaType.String],
score: (value) => typeof value === "number" && value >= 0
}),
(req, res) => {
res.json({ ok: true });
}
);API 📚
validateBody(schema)
Returns an Express middleware that validates req.body.
schema:SchemaValue
SchemaType
Use these constants instead of raw strings:
SchemaType.StringSchemaType.NumberSchemaType.BooleanSchemaType.ObjectSchemaType.ArraySchemaType.NullSchemaType.Any
SchemaValue
import type { SchemaValue } from "@r01al/express-body-validation";
const schema: SchemaValue = {
name: SchemaType.String,
age: SchemaType.Number
};Supported shapes:
- Primitives:
SchemaType.* - Objects:
{ user: { id: SchemaType.Number } } - Arrays:
[SchemaType.String]or[{ id: SchemaType.Number }] - Functions:
(value) => booleanto fully override validation at that node
Schema is validated before use. Invalid schemas return an error message instead of throwing.
Examples ✨
Array body
app.post(
"/tags",
validateBody([
SchemaType.String
]),
(req, res) => {
res.json({ ok: true });
}
);Nested object + arrays
validateBody({
order: {
id: SchemaType.String,
items: [
{
sku: SchemaType.String,
qty: SchemaType.Number
}
]
}
});Function override
validateBody({
password: (value) => typeof value === "string" && value.length >= 8
});Function validator details
- The function receives the raw value at that schema node.
- Return
trueto accept,falseto reject. - If the function throws, the error is caught and returned as a validation message.
Example with custom error message:
validateBody({
age: (value) => {
if (typeof value !== "number") {
throw new Error("age must be a number");
}
return value >= 18;
}
});Errors ⚠️
If validation fails, the middleware responds with HTTP 400:
{ "message": "missing field 'payload.user.name'" }or
{ "message": "invalid field 'payload.age': expected number, got string" }Build 🛠️
npm run buildTest 🧪
npm testNode Support 🟢
- Node
>=14(seepackage.jsonengines)
License 📝
ISC
