# @sertis/request-validator

> Express middleware for http request validation.

Latest version **1.0.11** (published 2020-04-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sertis/request-validator
pnpm add @sertis/request-validator
yarn add @sertis/request-validator
bun add @sertis/request-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.11 |
| Published | 2020-04-23 |
| First published | 2020-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 39.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sertis Co.,Ltd. |
| Maintainers | sertis-dev |

## Links

- npm: https://www.npmjs.com/package/@sertis/request-validator
- npm.io page: https://npm.io/package/@sertis/request-validator

## Dependencies (1)

- [@sertis/http-status](https://npm.io/package/@sertis/http-status.md) ^1.0.1

## Recent versions

- 1.0.11 (latest) — 2020-04-23
- 1.0.10 — 2020-04-23
- 1.0.9 — 2020-04-23
- 1.0.8 — 2020-04-23

## README

<a name="home"></a> `request-validator`
 ====

> Express middleware for http request validation.

## <a name="contents"></a> Contents
 - [Release](https://gitlab.com/sertis/datana-request-validator/blob/master/RELEASE.md)
 - [Installation](#installation)
 - [Example](#example)
 - [Api configuration](#config)
 - [Error response format](#error)

## <a name="installation"></a> Installation

```bash
npm i @sertis/request-validator
```

## <a name="example"></a> Example
> NOTE: Must use validator before your router middleware.

```javascript
const app = require('express')()
const Validator = require('request-validator')

const apiConfig = require('/path/to/apiConfig')
const validator = new Validator(apiConfig) //apiConfig is optional.

app.post('/', validator.validate(), (req, res) => {
  res.send('Hello!')
})

app.listen(8081, () => {
  console.log('Start listen on port 8081')
})
```
<br></br>
## <a name="config"></a> Api configuration 

| configuration | type | description | default |
| ------------- | ---- | ----------- | ------- |
| require | ```object``` | configuration for parameters must exists in the request body | ```{}``` |
| optional | ```object``` | configuration for parameters can exists in the request body | ```{}``` |
| allowNull | ```boolean``` | configuration for optional parameters to allow to keep ```null``` parameters in the request body | ```false``` |


```javascript
module.exports = {
  'METHOD /endpoint': {
    allowNull: true,
    require: {
      parameter: 'type',
    },
    optional: {
      parameter: 'type',
    },
  },
}
```

Configuration for object and array (1.0.8 or Higher)
```javascript
module.exports = {
  'METHOD /endpoint': {
    require: {
      objectA: 'object', //OR
      objectB: {
        parameter: 'type',
      },
      arrayA: 'array', //OR
      arrayB: ['type'],
    },
    optional: {
      objectA: 'object', //OR
      objectB: {
        parameter: 'type',
      },
      arrayA: 'array', //OR
      arrayB: ['type'],
    },
  },
}
```

#### <a name="type"></a> Available variable type
> NOTE: Extend from Javascript variable type.

- string
- number
- boolean
- object
- array (1.0.1 or Higher)

#### Configuration example

```javascript
module.exports = {
  'POST /deletenotebook': {
    require: {
      notebookName: 'string',
    },
  },
  'POST /importNotebook': {
    allowNull: true,
    optional: {
      notebookName: 'string',
      cells: [{
        title: 'string',
        code: 'string',
      }],
    },
  },
  'POST /createNotebook': {
    require: {
      notebookName: 'string',
    },
    optional: {
      noteBookDetail: 'string',
    },
  },
}
```

<br></br>
## <a name="error"></a> Error response format

- Require parameters validation failed error

    ```json
    {
      "error": {
        "code": 400,
        "status": "BAD_REQUEST",
        "message": "Require parameters validation failed",
        "details": [
          "Expected \"notebookName\" to be a \"string\", but got a \"undefined\""
        ]
      }
    }
    ```

- Optional parameters validation failed error

    ```json
    {
      "error": {
        "code": 400,
        "status": "BAD_REQUEST",
        "message": "Optional parameters validation failed",
        "details": [
          "Expected \"notebookName\" to be a \"string\", but got a \"number\""
        ]
      }
    }
    ```

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