1.0.2 • Published 3 years ago

egg-ajv-ex v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

egg-ajv-ex

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

 

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

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago