egg-valparams
web参数验证工具 For egg
查看 valparams 获取具体的参数定义.
Install
npm i egg-valparams --save
How to use
Usage
// config/plugin.js
exports.valparams = {
enable : true,
package: 'egg-valparams'
};
// config/config.default.js
exports.valparams = {
locale : 'zh-cn',
throwError: false
};
The basic usage
class XXXController extends app.Controller {
// ...
async XXX() {
const {ctx} = this;
ctx.validate({
system : {type: 'string', required: false, defValue: 'account', desc: '系统名称'},
token : {type: 'string', required: true, desc: 'token 验证'},
redirect: {type: 'string', required: false, desc: '登录跳转'}
});
// if (config.throwError === false)
if(ctx.paramErrors) {
// get error infos from `ctx.paramErrors`;
}
let params = ctx.params;
let {query, body} = ctx.request;
// ctx.params = validater.ret.params;
// ctx.request.query = validater.ret.query;
// ctx.request.body = validater.ret.body;
// ...
ctx.body = query;
}
// ...
}