Validate parameters via raml for egg.
npm install egg-raml-validateValidate parameters via RAML for egg.
``sh
npm install -S egg-raml-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.:`
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 methodExtentions to egg-validate
$3
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.$3
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.MIT