react 链式写法校验数据

使用方法

try {
    new Joi()
    .data('李四', '姓名').required().chinese()
    .data('张三', '姓名').required().chinese('名字只可以是中文哦!')
    .data('1', '性别').required().enum(['1', '2'])
    // 这里写正常逻辑
} catch (error) {
    // 这里提示错误消息
    console.log(error)
}

源码, 使用了moment

⚠️没怎么测试哦

import moment from 'moment'
export default class Joi {
    value = ''
    name = ''

    // 数据输入
    data(value, name = '') {
        this.value = value
        this.name = name
        return this
    }

    // 必填,不能为空
    required(message = `${this.name}不能为空`) {
        if (
            /^\s*$/g.test(this.value) ||
            this.value === null ||
            this.value === undefined
        ) {
            throw message
        }
        return this
    }

    // 最小长度
    minLength(length = 6, message = `${this.name}长度不能小于${length}位`) {
        if (this.value.length < length) {
            throw message
        }
        return this
    }

    // 最大长度
    maxLength(length = 6, message = `${this.name}长度不能超过${length}位`) {
        if (this.value.length > length) {
            throw message
        }
        return this
    }

    // 固定长度
    length(length = 6, message = `${this.name}长度必须为${length}位`) {
        if (this.value.length !== length) {
            throw message
        }
        return this
    }

    // 整数
    integet(message = `${this.name}必须为整数`) {
        if (Number(this.value) === 'NaN') {
            throw message
        }
        return this
    }

    // 数字
    number(message = `${this.name}要是数字格式`) {
        if (!/^\+?[1-9][0-9]+.?[0-9]*$/.test(this.value)) {
            throw message
        }
        return this
    }

    // 不等于
    not(num = 0, message = `${this.name}不可以等于${num}`) {
        if (Number(this.value) === num) {
            throw message
        }
        return this
    }

    // 等于
    eq(num = 0, message = `${this.name}要等于${num}`) {
        if (Number(this.value) !== num) {
            throw message
        }
        return this
    }

    // 相等
    equals(value, message = '两次密码不一致') {
        if (this.value !== value) {
            throw message
        }
        return this
    }

    // 大于
    gt(num = 0, message = `${this.name}要大于${num}`) {
        if (Number(this.value) <= num) {
            throw message
        }
        return this
    }

    // 大于或等于
    gte(num = 0, message = `${this.name}要大于或等于${num}`) {
        if (Number(this.value) < num) {
            throw message
        }
        return this
    }

    // 小于
    lt(num = 0, message = `${this.name}要小于${num}`) {
        if (Number(this.value) >= num) {
            throw message
        }
        return this
    }

    // 小于或等于
    lte(num = 0, message = `${this.name}要小于或等于${num}`) {
        if (Number(this.value) > num) {
            throw message
        }
        return this
    }

    // 之间, 大于并小于
    between(nums = [0, 100], message = `${this.name}要在${nums[0]}至${nums[1]}之间`) {
        let num = Number(this.value)
        if (num >= nums[0] && num <= nums[0]) {
            return this
        }
        throw message
    }

    // 最小
    min(num = 0, message = `${this.name}最小值为${num}`) {
        if (Number(this.value) < num) {
            throw message
        }
        return this
    }

    // 最大
    max(num = 100, message = `${this.name}最大值为${num}`) {
        if (Number(this.value) > num) {
            throw message
        }
        return this
    }

    // 手机号
    mobile(message = "手机号不正确") {
        if (!/^1[3-9]\d{9}$/.test(this.value)) {
            throw message
        }
        return this
    }

    // 年龄
    age(arr = [0, 129], message = "年龄不正确") {
        let age = parseInt(this.value, 10)
        if (age > arr[1] || age < arr[0]) {
            throw message
        }
        return this
    }

    // 日期
    date(format = 'YYYY-MM-DD', message = `${this.name || '日期'}不正确`) {
        if (this.value.length !== format.length) {
            throw message
        }
        if (!moment(this.value, format).isValid()) {
            throw message
        }
        return this
    }

    // 日期是否之前
    isBefore(date, message = `${this.name || '日期'}请选择${date}之前的日期`) {
        if (!moment(this.value).isBefore(date)) {
            throw message
        }
        return this
    }

    // 日期是否之后
    isAfter(date, message = `${this.name || '日期'}请选择${date}之后的日期`) {
        if (!moment(this.value).isAfter(date)) {
            throw message
        }
        return this
    }

    // 日期是否相同
    isSame(date, message = `${this.name || '日期'}请选择${date}`) {
        if (!moment(this.value).isSame(date)) {
            throw message
        }
        return this
    }

    // 日期是否之间
    isBetween(dates = [moment().format('YYYY-MM-DD'), moment().add('day', 1).format('YYYY-MM-DD')], message = `${this.name || '日期'}请选择${dates[0]}至${dates[1]}`) {
        if (!moment(this.value).isBetween(dates[0], dates[1])) {
            throw message
        }
        return this
    }

    // 字母
    letter(message = `${this.name}必须为英文`) {
        if (!/^[A-Za-z]+$/g.test(this.value)) {
            throw message
        }
        return this
    }

    // 中文
    chinese(message = `${this.name}必须为中文`) {
        if (!/^[\u4e00-\u9fa5]+$/g.test(this.value)) {
            throw message
        }
        return this
    }

    // 枚举
    enum(arr = [], message = `${this.name}值必须为${arr.join(',')}中的一个`) {
        if (arr.indexOf(this.value) === -1) {
            throw message
        }
        return this
    }

    // 身份证
    idCard(message = '身份证不合法') {
        if (this.value.length !== 18) {
            throw message
        }
        let city = ["11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82", "91"]
        if (!/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(this.value)) {
            throw message
        }
        if (city.indexOf(this.value.substr(0, 2)) === -1) {
            throw message
        }
        let id_array = [...this.value]
        //加权因子
        let factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        //校验位
        let parity = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
        let sum = 0
        for (let i = 0; i < 17; i++) {
            sum += parseInt(id_array[i], 10) * parseInt(factor[i], 10)
        }
        if (id_array[17].toUpperCase() !== parity[sum % 11].toUpperCase()) {
            throw message
        }
        return this
    }

    // 邮箱
    email(message = '邮箱不合法') {
        if (!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(this.value)) {
            throw message
        }
        return this
    }

    // 自定义正则
    regexp(reg, message = `${this.name}格式不正确`) {
        if (!reg.test(this.value)) {
            throw message
        }
        return this
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,097评论 1 32