egg-raml-validate

0.6.2 • Public • Published

egg-raml-validate

Validate parameters via RAML for egg.

Standard - JavaScript Style Guide Made by m31271n License - MIT

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 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:

{
    "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

Package Sidebar

Install

npm i egg-raml-validate

Weekly Downloads

1

Version

0.6.2

License

MIT

Last publish

Collaborators

  • m31271n_account