@robertoachar/whoops
v1.0.2
Published
An express middleware validator.
Downloads
4
Maintainers
Readme
whoops
Generated by OSS Project Generator.
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
| Action | Usage |
| ---------------------------------------- | ------------------- |
| Linting code | npm run lint
|
| Running unit tests | npm run jest
|
| Running code coverage | npm run coverage
|
| Running lint + tests | npm test
|
| Sending coverage results to Coveralls.io | npm run coveralls
|