egg-raml-validate
v0.6.2
Published
Validate parameters via raml for egg.
Downloads
7
Maintainers
Readme
Installation
npm install -S egg-raml-validate
# and its dependencies
npm install -S egg-validate
Usage
Import it and its dependencies via config/plugin.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 methodcontroller
- 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 forerrors
.
For example, when using egg-validate
, the response is:
{
"code": "invalid_param",
"errors": [
{
"code": "missing_field",
"field": "quantity",
"message": "required"
}
],
"message": "Validation Failed"
}
But, when using egg-raml-validate
, the response is:
{
"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