egg-ajv-ex
v1.0.2
Published
Ajv JSON schema validator based parameter validation plugin
Downloads
3
Readme
egg-ajv-ex
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.