npm package: @sertis/request-validator
Description: Express middleware for http request validation.
Author: Sertis Co.,Ltd.
Latest version: 1.0.11

 <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\""
        ]
      }
    }
    ```