https://github.com/robertoachar/whoops
An express middleware validator.
https://github.com/robertoachar/whoops
express middleware node validator
Last synced: 3 months ago
JSON representation
An express middleware validator.
- Host: GitHub
- URL: https://github.com/robertoachar/whoops
- Owner: robertoachar
- License: mit
- Created: 2017-08-28T05:42:58.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-02-20T17:19:08.000Z (over 8 years ago)
- Last Synced: 2025-09-14T04:57:43.319Z (10 months ago)
- Topics: express, middleware, node, validator
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@robertoachar/whoops
- Size: 47.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# whoops
Generated by [OSS Project Generator](http://bit.ly/generator-oss-project).
[![Travis Status][travis-badge]][travis-url]
[![AppVeyor Status][appveyor-badge]][appveyor-url]
[![CircleCI Status][circleci-badge]][circleci-url]
[![Coveralls Status][coveralls-badge]][coveralls-url]
[![NPM Version][npm-badge]][npm-url]
[![License][license-badge]][license-url]
> 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
```bash
$ npm install --save @robertoachar/whoops
```
# Usage
* Import whoops
```javascript
const express = require('express');
const whoops = require('@robertoachar/whoops');
```
* Initialize whoops
```javascript
const app = express();
app.use(whoops());
```
* Use whoops
```javascript
app.post('/', (req, res) => {
req.whoops.checkStringEmpty(req.body.name, 'Enter a name');
res.json({ name: req.body.name });
});
```
* Error Handling
```javascript
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
```bash
$ git clone https://github.com/robertoachar/whoops.git
```
* Installing dependencies
```bash
$ 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` |
# Author
[Roberto Achar](https://twitter.com/robertoachar)
# License
[MIT](https://github.com/robertoachar/whoops/blob/master/LICENSE)
[travis-badge]: https://travis-ci.org/robertoachar/whoops.svg?branch=master
[travis-url]: https://travis-ci.org/robertoachar/whoops
[appveyor-badge]: https://ci.appveyor.com/api/projects/status/github/robertoachar/whoops?branch=master&svg=true
[appveyor-url]: https://ci.appveyor.com/project/robertoachar/whoops
[circleci-badge]: https://circleci.com/gh/robertoachar/whoops/tree/master.svg?style=shield
[circleci-url]: https://circleci.com/gh/robertoachar/whoops
[coveralls-badge]: https://coveralls.io/repos/github/robertoachar/whoops/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/robertoachar/whoops?branch=master
[npm-badge]: https://img.shields.io/npm/v/@robertoachar/whoops.svg
[npm-url]: https://www.npmjs.com/package/@robertoachar/whoops
[license-badge]: https://img.shields.io/github/license/robertoachar/whoops.svg
[license-url]: https://opensource.org/licenses/MIT