# express-fastest-validator

> Fastest request validation library

Latest version **1.0.7** (published 2020-12-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install express-fastest-validator
pnpm add express-fastest-validator
yarn add express-fastest-validator
bun add express-fastest-validator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2020-12-05 |
| First published | 2020-11-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | FroxTrost |
| Maintainers | froxtrost |
| Keywords | Express, Request, node, Validation, validator, express validation |

## Links

- npm: https://www.npmjs.com/package/express-fastest-validator
- Repository: https://github.com/FroxTrost/express-request-fastest-validator
- Homepage: https://github.com/FroxTrost/express-request-fastest-validator#readme
- Issues: https://github.com/FroxTrost/express-request-fastest-validator/issues
- npm.io page: https://npm.io/package/express-fastest-validator

## Dependencies (1)

- [fastest-validator](https://npm.io/package/fastest-validator.md) ^1.9.0

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.0.7 (latest) — 2020-12-05
- 1.0.6 — 2020-11-29
- 1.0.5 — 2020-11-29
- 1.0.4 — 2020-11-28
- 1.0.2 — 2020-11-28
- 1.0.3 — 2020-11-28

## README

# express-fastest-validator

### 🚀 **Fastest express request validation library** 🚀

Error handling in a large project has always been a complex task in express projects. **express-fastest-validator** eases the pain of the developer in handling the invalid API requests.
**express-fastest-validator** is based on the fastest validations js library [fastest-validator](https://github.com/icebob/fastest-validator).

**express-fastest-validator** provides a request validation middleware which you can plug in your express routes which will validate the express request and throw a Error if any validation rules fails. Yes !! it is this easy to handle invalid requests.

## Installation

Install with npm

```
npm install express-fastest-validator
```

Install with yarn

```
yarn add express-fastest-validator
```

## Request Properties

express-fastest-validator supports multiple [express request properties](https://expressjs.com/en/api.html#req)

- body
- headers
- params
- qs
- query

## Schema Definition

express-fastest-validator uses [fastest-validator](https://github.com/icebob/fastest-validator) which is the the **fastest JS validator library for NodeJS | Browser**.

```javascript
// Schema for validating user login API
const loginSchema = {
  body: {
    username: { type: 'string', min: 3, max: 15 },
    password: { type: 'string', min: 8 },
  },
};
```

Read complete
**[docs](https://github.com/icebob/fastest-validator)
for field types and built-in validators**

## Validating API

You just need to define your schema and plug it into your API route. This is all you need to do.

```javascript
const { express } = require('express');
const { validator } = require('express-fastest-validator');

const app = express();

const loginSchema = {
  body: {
    username: { type: 'string', min: 3, max: 15 },
    password: { type: 'string', min: 8 },
  },
};

app.post('/api/user', validator(loginSchema), userController.login);
```

## Error Handling Middleware

[Express Middleware](https://expressjs.com/en/guide/using-middleware.html) provides the power of handling the request response cycle with ease.

Middleware can be used to handle all the API errors within a single peace of code. You can use a middleware to handle all the API errors in your project. **You can define your error handling middleware after defining all your routes**. This is because we want the error handling middleware to run just before sending the response to check weather the API is returning a valid response or an error.

Read more about [Error-handling Middleware](https://expressjs.com/en/guide/using-middleware.html#middleware.error-handling)

```javascript
const errorHandler = (err, req, res, next) => {
  /*
   * FXValidationError will be raised by express-fastest-validator if the request is invalid.
   * FXValidationError.description is an array describing validation errors.
   */
  if (err instanceof FXValidationError) {
    res.status(400).send(err.description);
  }

  // APIError is your custom Error class for api errors.
  if (err instanceof MyAPIError) {
    res.status(err.status).send(err.message);
  } else {
    res.status(500).send('something went wrong');
  }
};

// Now you can use this plug this middleware in your express app.
app.use(errorHandler);
```

## Custom Error Handling Example

```javascript
const express = require('express');
const bodyParser = require('body-parser');
const { validator, FXValidationError } = require('express-fastest-validator');

const PORT = 8000;

const schema = {
  params: {
    username: { type: 'string', min: 3, max: 15 },
  },
  body: {
    age: { type: 'number', positive: true, integer: true },
  },
};

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/user/:username', validator(schema), (req, res) => {
  const { age } = req.body;
  const { username } = req.params;
  res.status(200).send(`Welcome 🔥${username}🔥, you are ${age} years old`);
});

// Set the value of errorHandlerMiddleware to true
app.set('errorHandlerMiddleware', true);

// Error Handling Middleware
app.use((err, req, res, next) => {
  if (err instanceof FXValidationError) {
    // Handle request invalidation error
    res.status(400).send(err.description);
  } else {
    // Handle all the others except request invalidation errors.
    res.status(500).send('something went wrong');
  }
});

app.listen(PORT, () => {
  console.log(`⚡️[server]: Server is running at http://localhost:${PORT}`);
});
```

---
_Source: https://npm.io/package/express-fastest-validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
