egg-valparams
TypeScript icon, indicating that this package has built-in type declarations

1.4.5 • Public • Published

egg-valparams

web参数验证工具 For egg

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

查看 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;
  }
  // ...
}

License

MIT

Package Sidebar

Install

npm i egg-valparams

Weekly Downloads

465

Version

1.4.5

License

MIT

Unpacked Size

20.1 kB

Total Files

7

Last publish

Collaborators

  • 780