• async-validator.js数据校验器


    文档:

    安装

    npm i async-validator
    
    • 1

    示例

    // demo.mjs
    // node(v16.14.0)
    
    // import Schema from 'async-validator';
    
    // fix: 文档给出的引入方式报错
    import asyncValidator from 'async-validator';
    const Validator = asyncValidator.default;
    
    // 定义校验规则
    const rules = {
      name: {
        type: 'string',
        required: true,
        validator: (rule, value) => value === 'muji',
      },
      age: {
        type: 'number',
        asyncValidator: (rule, value) => {
          return new Promise((resolve, reject) => {
            if (value < 18) {
              reject('too young'); // reject with error message
            } else {
              resolve();
            }
          });
        },
      },
    };
    
    const validator = new Validator(rules);
    
    let data = { name: 'Tom', age: 16 };
    
    validator
      .validate(data)
      .then(() => {
        // validation passed or without error message
        console.log('validate passed');
      })
      .catch(({ errors, fields }) => {
        console.log(errors, fields);
      });
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    打印出的校验信息

    [
      { message: 'name fails', fieldValue: 'Tom', field: 'name' },
      { message: 'too young', fieldValue: 16, field: 'age' }
    ] {
      name: [ { message: 'name fails', fieldValue: 'Tom', field: 'name' } ],
      age: [ { message: 'too young', fieldValue: 16, field: 'age' } ]
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    rule的属性

    type
    required
    pattern
    len
    enum
    min/max
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    可以使用的 Type

    类型描述
    stringMust be of type string. This is the default type.
    numberMust be of type number.
    booleanMust be of type boolean.
    methodMust be of type function.
    regexpMust be an instance of RegExp or a string that does not generate an exception when creating a new RegExp.
    integerMust be of type number and an integer.
    floatMust be of type number and a floating point number.
    arrayMust be an array as determined by Array.isArray.
    objectMust be of type object and not Array.isArray.
    enumValue must exist in the enum.
    dateValue must be valid as determined by Date
    urlMust be of type url.
    hexMust be of type hex.
    emailMust be of type email.
    anyCan be any type.
  • 相关阅读:
    还在寻找PDF压缩方法?这个方法值得一试
    前端点击获取验证码,进行60秒倒计时
    【网络篇】第十篇——线程池版的TCP网络程序
    基于 Redisson 和 Kafka 的延迟队列设计方案
    Linux基础
    【学习笔记】《Python深度学习》第二章:神经网络的数学基础
    无人机与视觉结合项目
    图形学-几何-网格操作
    Java开发快递物流项目
    Python 中的闭包和自由变量
  • 原文地址:https://blog.csdn.net/mouday/article/details/125496497