1.0.2 • Published 3 years ago
myzod-express v1.0.2
MyZod Express library
Express middleware to handle requests validation by myzod library. Library can be used for versions 1.x.
Installation
npm install myzod-expressUsage
Library provided function validateRequest which return RequestHandler middleware.
validateRequest(field: RequestField, schema: MyZodSchema, options?: Options): RequestHandlerfield(at this moment -body,params,query) parameter specify whichRequestfield should be use during validation,schemaparameter ismyzodschema,options(optional) parameter stores additional informations.
Additional options
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.
Usage example
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');
});Project commands
Brief explanation of project NPM commands.
Build
prebuildremoveslibdirectory before each building
npm run buildRun tests
npm testRun tests with coverage
npm run coverage