@simongrayman/express-guard
v0.1.0
Published
Validation, Sanitization and Error Handling Kit for Express.js
Maintainers
Readme
express-guard
Validation, Sanitization & Error Handling Kit for Express.js
express-guard is a lightweight, modern, TypeScript-first package that minimizes validation and error-handling boilerplate in Express applications.
Features
- Powerful request validation powered by Zod
- Centralized error handler with standardized JSON responses
- Custom error classes (
ValidationError,AppError) - Full TypeScript support with strong type inference
- Automatic data transformation after validation
- Debug mode for development
- Lightweight and performant
Installation
pnpm add express-guard
# or
npm install express-guardcreat Guard:
import express from 'express'; import { createGuard } from 'express-guard'; import { z } from 'zod';
const app = express(); const guard = createGuard({ debug: true, // Enable debug logs in development validationErrorStatus: 422 // Default validation error status code });
app.use(express.json());
Use validation:
const createUserSchema = z.object({ name: z.string().min(3, "Name must be at least 3 characters long"), email: z.string().email("Invalid email address"), age: z.number().min(18).optional(), });
app.post( '/users', guard.validate({ body: createUserSchema }), // Validate request body (req, res) => { res.json({ success: true,WSA message: "User created successfully", data: req.body }); } );
Error Handler:
// This middleware must be registered after all routes app.use(guard.errorHandler());
