An open API service indexing awesome lists of open source software.

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.

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