egg-raml-validate
Version:
Validate parameters via raml for egg.
111 lines (81 loc) • 2.25 kB
Markdown
<h1 align="center">egg-raml-validate</h1>
<p align="center">Validate parameters via RAML for egg.</p>
<p align="center">
<a href="http://standardjs.com/" target="_blank"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat" alt="Standard - JavaScript Style Guide"></a>
<a href="http://m31271n.com/" target="_blank"><img src="https://img.shields.io/badge/made%20by-m31271n-brightgreen.svg?style=flat" alt="Made by m31271n"></a>
<img src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat" alt="License - MIT">
</p>
## Installation
```sh
npm install -S egg-raml-validate
# and its dependencies
npm install -S egg-validate
```
## Usage
Import it and its dependencies via `config/plugin.js`:
```js
module.exports = {
...
'validate': {
enable: true,
package: 'egg-validate'
},
'raml-validate': {
enable: true,
package: 'egg-raml-validate'
}
...
}
```
Config it via `config/config.<env>.js`:
```
module.exports = {
...
ramlValidate: {
ramlFile: '/absolute/path/to/raml/file'
}
...
}
```
## Extentions to RAML
Add two annotations to Method:
+ `middlewares` - apply middlewares to current method
+ `controller` - apply controller to current method
## Extentions to egg-validate
### Why?
`egg-raml-validate` will check multiple types of parameters at one time, different from what `egg-validate` does. Therefore, we have to find a way to distinguish different types of validate error.
### Extensions
Extensions of `egg-validate`:
+ Add `in` field for `errors`.
For example, when using `egg-validate`, the response is:
```json
{
"code": "invalid_param",
"errors": [
{
"code": "missing_field",
"field": "quantity",
"message": "required"
}
],
"message": "Validation Failed"
}
```
But, when using `egg-raml-validate`, the response is:
```json
{
"code": "invalid_param",
"errors": [
{
"code": "missing_field",
"field": "quantity",
"in": "query",
"message": "required"
}
],
"message": "Validation Failed"
}
```
You can see, `in` field let you know where the missing field in.
## LICENSE
MIT