create-express-quickstart
Version:
Create a Node.js app for building production-ready RESTful APIs using Express, by running one command
17 lines (14 loc) • 417 B
JavaScript
import { AppError } from '../utils/AppError.js'
/**
* Validation middleware using Zod schema
* @param {ZodSchema} schema - Zod validation schema to validate the request body.
*/
const validate = (schema) => (req, res, next) => {
try {
req.body = schema.parse(req.body)
next()
} catch (err) {
next(new AppError(`${err.errors.map((e) => e.message).join(', ')}`, 400))
}
}
export default validate