Skip to content
关注公众号,获取新课通知

参数验证


插件地址:

https://www.npmjs.com/package/egg-valparams

安装

shell
npm i egg-valparams --save

配置

js
// config/plugin.js
valparams : {
  enable : true,
  package: 'egg-valparams'
},
// config/config.default.js
config.valparams = {
    locale    : 'zh-cn',
    throwError: false
};

在控制器里使用

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

ValParams API 说明

参数验证处理

Valparams.setParams(req, params, options);

具体参数验证处理

js
const Valparams = require("path/to/Valparams[/index]");
Valparams.locale("zh-cn");

function list(req, res, next) {
  let validater = Valparams.setParams(req, {
    sysID: { alias: "sid", type: "int", required: true, desc: "所属系统id" },
    page: {
      type: "int",
      required: false,
      defValue: 1,
      range: { min: 0 },
      desc: "页码",
    },
    size: { type: "int", required: false, defValue: 30, desc: "页面大小" },
    offset: { type: "int", required: false, defValue: 0, desc: "位移" },
  }, {
    choices: [{ fields: ["sysID", "page"], count: 1, force: false }],
  });
  if (validater.err && validater.err.length) {
    console.log(validater.err);
  } else {
    console.log(validater);
    //{ query: { page: 1, size: 30 },
    //  body: {},
    //  params: { sysID: 2 },
    //  all: { sysID: 2, page: 1, size: 30 },
    //  err: null }
    //  raw: { query: { page: 1, size: 30 },
    //         body: {},
    //         params: { sid: 2 },
    //       }
    //}
    //do something
  }
}

返回支持的类型列表

js
Valparams.vType = {
  ALL: "all",
  STRING: "string",
  ARRAY: "array",
  DATE: "date",
  INT: "int",
  FLOAT: "float",
  LETTER: "letter",
  NUMBER: "number",
  IP: "ip",
  EMAIL: "email",
  PHONE: "phone",
  URL: "url",
  JSON: "json",
  BOOL: "bool",
  NULL: "null",
  RANGE: "range",
  DATERANGE: "dateRange",
  INTRANGE: "intRange",
  FLOATRANGE: "floatRange",
  NUMBERRANGE: "numberRange",
};
自定义本地化文件

Valparams.defineLocale(key, value);

ParamTypeDescriptionExample
keyString语言标识zh-cn
valueObject本地化内容,可配置内容有 em_type, em_minmax, em_reg, em_in, em_schema, em_required, em_range_relation, em_choices, em_equals, em_compares, em_cases---
更新已有本地化文件内容

Valparams.updateLocale(key, value);

参数含义同 defineLocale

获取本地化文件内容

Valparams.localeData(key);

ParamTypeDescriptionExample
keyString语言标识zh-cn
列出已加载的本地化文件

Valparams.locales(key);

目前已有 en 、 zh-cn

ParamTypeDescriptionExample
keyString语言标识zh-cn
设置使用的本地化文件

Valparams.locale(locale); 如: Valparams.locale('zh-cn');