Express middleware to handle requests validation by myzod library. Library can be used for versions 1.x
.
npm install myzod-express
Library provided function validateRequest
which return RequestHandler
middleware.
validateRequest(field: RequestField, schema: MyZodSchema, options?: Options): RequestHandler
field
(at this moment -body
,params
,query
) parameter specify whichRequest
field should be use during validation,schema
parameter ismyzod
schema,options
(optional) parameter stores additional informations.
By default, middleware pass ValidationError
to the next()
function. It can be changed by additional option createAndReturnResponse
, which pass ValidationError
to response.send()
function. If defaultResponseFunc
was provide, response.send()
will return result of this function.
Response status code can be changed by defaultResponseCode
.
MyZod additional options can be passed by myzodOptions
.
const schema = myzod.object({
id: myzod.string(),
});
const app: Express = express();
app.get('/', [validateRequest('params', schema), (req, res) => {
return 'working';
}]);
app.listen(2137, () => {
console.log('App listen');
});
Brief explanation of project NPM commands.
prebuild
removeslib
directory before each building
npm run build
npm test
npm run coverage