1.0.2 • Published 2 years ago

myzod-express v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

MyZod Express library

Express middleware to handle requests validation by myzod library. Library can be used for versions 1.x.

Installation

  npm install myzod-express

Usage

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 which Request field should be use during validation,
  • schema parameter is myzod schema,
  • 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

prebuild removes lib directory before each building

  npm run build

Run tests

  npm test

Run tests with coverage

  npm run coverage