Skip to content

Latest commit

 

History

History
464 lines (393 loc) · 13.9 KB

File metadata and controls

464 lines (393 loc) · 13.9 KB

前端页面 - 表单中的验证规则

namespace nasl.validation {
    /**
     * @group 常用规则
     * @group 空值判断规则
     * @description 必填,空值指空字符串 ''、undefined、null
     */
    declare function required(): Boolean;

    /**
     * @group 常用规则
     * @group 空值判断规则
     * @description 必填,在 required 的基础上,字符串不能全为空白(空格、回车、换行、制表符等),也不能是空 List、空 Map、空对象
     */
    declare function filled(): Boolean;

    /**
     * @group 空值判断规则
     * @description 不能为空,支持任意类型。包括 null、undefined、空字符串、空 List、空对象、空 Map;不包括含空白字符的字符串
     */
    declare function notEmpty(): Boolean;

    /**
     * @group 空值判断规则
     * @description 必须为空,即 notEmpty 取反
     */
    declare function empty(): Boolean;

    /**
     * @group 常用规则
     * @group 特定场景判断规则
     * @description 输入内容为合法手机号
     * @param locale: 所在地区,可以是一个字符串或字符串集合,例如 zh-CN,ja-JP 等;常量 null 可匹配所有国家(地区)。
     * @param strict: 是否检验国家代号。如果为 true,则必须以"+国家码"开头,如果为 false,则"+国家码"可有可无
     */
    declare function mobile(locale?: String | String[], strict?: Boolean): Boolean;

    /**
     * @group 常用规则
     * @group 特定场景判断规则
     * @description 校验邮箱格式,必须为正确的邮箱
     */
    declare function email(): Boolean;

    /**
     * @group 常用规则
     * @group 范围判断规则
     * @description 长度不得超过指定数字;List、Map 按元素个数比较,对象按属性个数比较,null、undefined 直接返回 false,其余转为字符串后按字符串长度比较
     * @param max: 最大长度
     */
    declare function maxLength(max: Integer): Boolean;

    /**
     * @group 范围判断规则
     * @description 长度不得少于指定数字;List、Map 按元素个数比较,对象按属性个数比较,null、undefined 直接返回 false,其余转为字符串后按字符串长度比较
     * @param min: 最小长度
     */
    declare function minLength(min: Integer): Boolean;

    /**
     * @group 范围判断规则
     * @description 长度不得超过指定数字(包括边界);List、Map 按元素个数比较,对象按属性个数比较,null、undefined 直接返回 false,其余转为字符串后按字符串长度比较
     * @param min: 最小长度
     * @param max: 最大长度
     */
    declare function rangeLength(min: Integer, max: Integer): Boolean;

    /**
     * @group 常用规则
     * @group 范围判断规则
     * @description 不得大于指定的值;支持数字、字符串(字典序)、日期、时间、日期时间
     * @param max: 可取到的最大值
     */
    declare function max(max: Integer | Decimal | String | Date): Boolean;
    
    /**
     * @group 范围判断规则
     * @description 不得小于指定的值;支持数字、字符串(字典序)、日期、时间、日期时间
     * @param min: 可取到的最小值
     */
    declare function min(min: Number | String | Date): Boolean;

    /**
     * @group 常用规则
     * @description 在指定的范围内(包括边界);支持数字、字符串(字典序)、日期、时间、日期时间
     * @param min: 可取到的最小值
     * @param max: 可取到的最大值
     */
    declare function range(min: Integer | Decimal | String | Date, max: Integer | Decimal | String | Date): Boolean;

    /**
     * @group 常用规则
     * @group 类型判断规则
     * @description 必须为整数
     */
    declare function integer(): Boolean;

    /**
     * @group 常用规则
     * @group 正则判断规则
     * @description 使用正则表达式判断

     * @param re: 正则表达式,例如 /^[a-z]/g
     * @example regex(/^[a-z]/g)
     */
    declare function regex(re: RegExp): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 必须与 arg 完全一致,包括类型、对象的内存地址,使用 === 比较
     */
    declare function is(arg: Any): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 与 arg 不完全一致
     */
    declare function isNot(arg: Any): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 与 arg 相等,例如 arg 和其拷贝
     */
    declare function equals(arg: Any): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 与 arg 不相等
     */
    declare function notEquals(arg: Any): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 验证逻辑同equals,错误信息专用于密码的二次确认
     * @message 两次输入的密码不一致
     */
    declare function confirmed(arg: Any): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 必须包含 List 集合中的所有项
     */
    declare function includes(args: List): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 不得包含 List 集合中的任何项
     */
    declare function excludes(args: List): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 必须为 List 集合中的某一项
     */
    declare function included(args: List): Boolean;

    /**
     * @group 相等或包含判断规则
     * @description 不得为 List 集合中的任何项
     */
    declare function excluded(args: List): Boolean;

    /**
     * @group 常用规则
     * @group 相等或包含判断规则
     * @description 不得为 List 集合中的任何项,若存在则提示:***已存在,专用于判重
     */
    declare function unique(arr: List): Boolean;

    /**
     * @group 类型判断规则
     * @description 必须为数字
     * @param noSymbols: 是否只允许小数。如果为true,不能包含+,-,.等符号
     */
    declare function numeric(noSymbols?: Boolean): Boolean;

    /**
     * @group 类型判断规则
     * @description 整数或小数
     * @param force: 是否只允许小数
     * @param digits: 小数位数范围,格式为1,3。默认为1,
     */
    declare function decimal(force?: Boolean, digits?: String): Boolean;

    // 字母数字判断规则
    /**
     * @group 字母数字判断规则
     * @description 只能为字母
     */
    declare function alpha(): Boolean;

    /**
     * @group 字母数字判断规则
     * @description 只能为字母或数字
     */
    declare function alphaNum(): Boolean;

    /**
     * @group 字母数字判断规则
     * @description 必须由字母或下划线组成
     */
    declare function alphaDash(): Boolean;

    /**
     * @group 字母数字判断规则
     * @description 必须由字母、数字或下划线组成
     */
    declare function alphaNumDash(): Boolean;

    /**
     * @group 字母数字判断规则
     * @description 必须由字母或空格组成
     */
    declare function alphaSpaces(): Boolean;

    /**
     * @group 字母数字判断规则
     * @description 不能出现大写字母
     */
    declare function lowerCase(): Boolean;

    /**
     * @group 字母数字判断规则
     * @description 不能出现小写字母
     */
    declare function upperCase(): Boolean;

    // 特定场景判断规则
    /**
     * @group 特定场景判断规则
     * @description 必须为正确的邮箱
     */
    declare function email(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 必须为正确的IP
     */
    declare function ip(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 必须为正确的端口
     */
    declare function port(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 需要输入半角字符
     */
    declare function halfWidth(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 必须输入全角字符
     */
    declare function fullWidth(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 必须输入正确的 MAC 地址
     */
    declare function macAddress(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 必须输入ascii字符
     */
    declare function ascii(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 必须输入base64编码
     */
    declare function base64(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串的字节长度范围限制
     * @param min: 最小字节长度
     * @param max: 最大字节长度
     */
    declare function byteLength(min: Integer, max: Integer): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 必须输入dataURI 编码
     */
    declare function dataURI(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入数字能被divisor整除
     * @param divisor: 整除数
     */
    declare function divisibleBy(divisor: Integer): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入编码符合algorithm哈希算法,
     * @param algorithm: 算法名称,支持md4、md5、sha1、sha256、sha384、sha512、ripemd128、ripemd160、tiger128、tiger160、tiger192、crc32以及crc32b
     */
    declare function hash(algorithm: String): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入编码符合md5算法
     */
    declare function md5(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入数字是十六进制
     */
    declare function hex(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串是十六进制颜色码
     */
    declare function hexColor(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入是合法信用卡号
     */
    declare function creditCard(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入合法的全限定域名
     */
    declare function fqdn(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入内容必须是一个合法IP或全限定域名
     */
    declare function ipOrFQDN(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入内容必须是正确的IP段,如130.5.5.25/24
     * @param version: IP版本,接受4或6
     */
    declare function ipRange(version?: Integer): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入内容是一个合法的国际标准书号(ISBN)
     * @param version: ISBN版本,接受10或13
     */
    declare function isbn(version?: Integer): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入内容是一个合法的国际标准连续出版物号(ISSN)
     */
    declare function issn(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入内容是一个合法的国际证券识别码(ISIN)
     */
    declare function isin(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入内容为合法的ISO8601日期
     * @param strict: 是否检测闰年日期,若strict为true,则2019-02-29属于非法日期
     */
    declare function iso8601(strict?: Boolean): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入内容是一个合法的ISO-31661 Alpha-2国家代码
     */
    declare function iso31661Alpha2(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入内容是一个合法的ISO-31661 Alpha-3国家代码
     */
    declare function iso31661Alpha3(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串可以被解析为JSON格式
     */
    declare function json(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串是合法的JSON Web Token
     */
    declare function jwt(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串为合法的经纬度坐标
     */
    declare function latLong(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串为合法的MongoDB对象的ID
     */
    declare function mongoId(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串为合法的邮政编码
     * @param locale: 所在地区,例如CN,JP等,详情请参阅 https://www.npmjs.com/package/validator 中的 isPostalCode
     */
    declare function postalCode(locale?: String | String[]): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串为合法的URL格式,如http://aa.bb
     */
    declare function url(): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串为合法的UUID
     * @param version: 采用的UUID版本,接受3,4,5,不填默认匹配所有版本
     */
    declare function uuid(version?: Integer): Boolean;

    /**
     * @group 特定场景判断规则
     * @description 输入字符串为合法的中文内容
     */
    declare function chinese(): Boolean;
}

示例如下:

function someView() {
    let existingNames: List<String>;

    onCreated(function init() {
        existingNames = app.logics.loadExistingNames();
        return;
    })

    return <>
        <ElInput
            modelValue={$sync(name1)}
            placeholder="请输入名称"
            rules={[filled(), maxLength(20), unique(existingNames)]}
            />
        <ElInput
            modelValue={$sync(email1)}
            placeholder="请输入邮箱"
            rules={[email()]}
            />
    </>
}