egg-validate
Validate plugin for egg.
See parameter for more information such as custom rule.
Install
$ npm i egg-validate --save
Usage
// config/plugin.js
exports.validate = {
enable: true,
package: 'egg-validate',
};
Validate Request Body
// app/controller/home.js
exports.index = function* () {
this.validate({ id: 'id' }); // will throw if invalid
// or
const errors = this.validator.validate({ id: 'id' }, this.request.body);
};
Extend Rules
- app.js
app.validator.addRule('jsonString', (rule, value) => {
try {
JSON.parse(value);
} catch (err) {
return 'must be json string';
}
});
Questions & Suggestions
Please open an issue here.