egg-ajv-ex

1.0.2 • Public • Published

egg-ajv-ex

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Ajv logo

 

Ajv JSON schema validator based parameter validation plugin for egg.
See Ajv for more information

Install

$ npm i egg-ajv-ex --save

Usage

// {app_root}/config/plugin.js
exports.ajvEx = {
  enable: true,
  package: 'egg-ajv-ex',
};

Configuration

// {app_root}/config/config.default.js
exports.ajvEx = {
  allErrors: true,
  messages: false,
  localize: 'zh',
  // errorText: true, // 
  // errorTextSeparator: ',',
};

see config/config.default.js for more detail.
View all ajv configurations.

Validate Request Body

// app/controller/home.js
class HomeController extends Controller {
  async index() {
    const { ctx, app } = this;
    const schema = {
      type: "object",
      properties: {
        foo: {type: "integer"},
        bar: {type: "string"}
      },
      required: ["foo"],
      additionalProperties: false
    }
    ctx.validate(schema); // will throw if invalid
    ctx.body = ctx.request.body;
  }
}
module.exports = HomeController;

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i egg-ajv-ex

Weekly Downloads

8

Version

1.0.2

License

MIT

Unpacked Size

8.32 kB

Total Files

7

Last publish

Collaborators

  • hankexu