DengSihan / inspur-validator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inspur Validator

使用于 Inspur 业务中的 js 用户输入验证器

使用

yarn add inspur-validator
import { validate } from 'inspur-validator';

validate(
    '18600000000',
    ['phone']
);
// {isValid: true}

validate(
    1000,
    ['number', 'min:10', 'max:100']
);
// {isValid: false, error: ':attr不能大于 100'}

validate(
    new File(), // 用户上传的文件
    [
        'file',
        'max:' + 1024 * 1024 * 10,
        'extension:jpg,png'
    ]
);
// {isValid: false, error: ':attr不能大于 10 MB'}
// {isValid: false, error: ':attr仅限 jpg, png 格式'}

API

validate(
    value: UserInputValue,
    rules: ValidateRule[],
    message: CustomErrorMessage = {},
): ValidateResult
参数 类型 描述 定义
value UserInputValue 用户输入的值 string | number | boolean | File
rules ValidateRule[] 验证规则 (UserInputType | RequiredValidateRule | PresetValidateRule | CommonValidateRule | CustomValidateRule| string)[]
message CustomErrorMessage 自定义错误消息 Record<string, string>

验证规则

校验规则分为 5 类

  1. 必填 (required) / 非必填 (默认值)

    validate(
        '',
        ['required']
    );
    // {isValid: false, error: ':attr不能为空'}
  2. 输入值类型:string(默认) / number / boolean / file

    validate(
        '1000',
        ['max:3']
    );
    // {isValid: false, error: ':attr不能多于 3 个字符'}
    
    validate(
        1000,
        ['number', 'max:3']
    );
    // {isValid: false, error: ':attr不能大于 3'}
  3. 预设规则

    规则 描述 定义
    phone 手机号码 /^1[3456789]\d{9}$/
    email 邮箱 /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
    id-card 身份证号码 /(\^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(\^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/
    social-credit-code 统一社会信用代码 /^[0-9A-Z]{18}$/
    no-emoji 禁止输入表情符号 /(\p{Emoji_Presentation}|\p{Extended_Pictographic})/gu
    no-space 禁止输入空格 /^\S+$/
    json JSON 字符串 JSON.parse
    password 密码 长度 8-20,必须包含数字、大小写字母、特殊字符、不含空格、不含表情
    password-confirm:${user-input} 确认密码 ${user-input} 的值相同
  4. 通用规则

    • string 类型
    规则 描述 定义
    min:${number} 最小长度 value.length >= number
    max:${number} 最大长度 value.length <= number
    length:${number} 固定长度 value.length === number
    regex:${pattern} 正则表达式 new RegExp(pattern).test(value)
    • number 类型
    规则 描述 定义
    min:${number} 最小值 value >= number
    max:${number} 最大值 value <= number
    • file 类型
    规则 描述 定义
    max:${number} 最大文件大小 value.size <= number
    extension:${ext1},${ext2} 文件扩展名 /\.(ext1|ext2)$/
  5. 自定义规则

    (value: UserInputValue): ValidateResult;
    validate(
        '1000',
        [
            (value) => {
                if (value !== '1000') {
                    return {
                        isValid: false,
                        error: '请输入 1000'
                    };
                }
                return {
                    isValid: true
                };
            }
        ]
    );

自定义错误消息

validate(
    '1000',
    ['required', 'max:3'],
    {
        'required': '不能为空',
        'max:3': '太大了'
    }
);
// {isValid: false, error: '太大了'}

types

types

About


Languages

Language:TypeScript 100.0%