1.0.2 • Published 6 years ago

@robertoachar/whoops v1.0.2

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

whoops

Generated by OSS Project Generator.

Travis Status AppVeyor Status CircleCI Status Coveralls Status NPM Version License

An express middleware validator.

This validator provides the following features:

  • checkArray(value, message): check if value is an array
  • checkArrayEmpty(value, message): check if value is an array and not empty
  • checkBoolean(value, message): check if value is a boolean
  • checkNumber(value, message): check if value is a number
  • checkNumberEmpty(value, message): check if value is a number and not zero or negative
  • checkObjectId(value, message): check if value is an ObjectId (mongoose)
  • checkString(value, message): check if value is a string
  • checkStringEmpty(value, message): check if value is a string and not empty

Installation

  • Install package
$ npm install --save @robertoachar/whoops

Usage

  • Import whoops
const express = require('express');
const whoops = require('@robertoachar/whoops');
  • Initialize whoops
const app = express();
app.use(whoops());
  • Use whoops
app.post('/', (req, res) => {
  req.whoops.checkStringEmpty(req.body.name, 'Enter a name');

  res.json({ name: req.body.name });
});
  • Error Handling
app.use((err, req, res, next) => {
  if (err && err.name === 'WhoopsError') {
    return res.status(400).json({ message: err.message }); // Enter a name
  }

  next(err);
});

Development

  • Cloning the repo
$ git clone https://github.com/robertoachar/whoops.git
  • Installing dependencies
$ npm install
  • Running scripts
ActionUsage
Linting codenpm run lint
Running unit testsnpm run jest
Running code coveragenpm run coverage
Running lint + testsnpm test
Sending coverage results to Coveralls.ionpm run coveralls

Author

Roberto Achar

License

MIT