@zkboys/egg-validator-async
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

egg-validator-async

NPM version Test coverage Known Vulnerabilities npm download

基于 async-validator 提供数据校验方法。

安装

$ npm i @zkboys/egg-validator-async --save

配置

// config/plugin.js
exports.validate = {
    enable: true,
    package: '@zkboys/egg-validator-async',
};

@zkboys/egg-validator-async 支持 async-validator 的所有配置项,查看 async-validator 文档 获取配置项的更多信息。

// config/config.default.js
exports.validate = {
    // convert: false,
};

使用方法

  • await ctx.validate(descriptor[, data]) data默认 ctx.request.body
  • app.validator.addRule(type, customerRule) app.validator.addRules({type: rule, type2: rule2}) 可以添加自定义校验type
  • 规则写法:
const descriptor = {
    name: {required: true, message: '姓名必填!'},
    age: [
        {required: true, message: '年龄必填!'},
        {type: 'number', message: '年龄必须是数字'},
    ],
    enabled: {type: 'boolean', message: '必须是boolean值'},
    mobile: {type: 'mobile'}, // 会转为 {...customerRule}
    mobile2: 'mobile?', // 自定义type 会转为 {...customerRule, required: false}
    job: 'string', // 会转为 {type: string, required: true}
    position: 'string?', // 会转为 {type: string, required: false}
}

Package Sidebar

Install

npm i @zkboys/egg-validator-async

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

13.7 kB

Total Files

7

Last publish

Collaborators

  • shubinwang