async-validator icon indicating copy to clipboard operation
async-validator copied to clipboard

多类型验证 + 弱类型兼容

Open flowingfate opened this issue 8 years ago • 10 comments

  1. 如果希望数组中的值全都采用同样的规则来校验,对此,能否提供这样的支持,例如:
rules = {
  names: { type: 'array', arrayItems: { type: 'string' } }
}

这样来指定,所有数组的值都只能是字符串

  1. API 调用中经常遇到这种情况:传参时可以传 字符串 也可以传 字符串数组,例如:
// 单入参
abridge.showActionSheet({
  items: '菜单名',
})
// 数组入参
abridge.showActionSheet({
  items: ['菜单1', '菜单2', '菜单3', '菜单4'],
})

对此,能否提供多类型校验功能,例如:

rules = {
  items: { type: ['string', 'array'], arrayItems: { type: 'string' } }
}

如果 type 指定为数组,则 items 满足其一都可以

  1. 数字存在弱类型判断问题,现有的逻辑中,如果声明了 type: 'number', "1234"会被判定为不合法,然而在使用过程中,这种情况很难避免,对此,能否提供一个弱类型支持,例如:
rules = {
  phone: { type: 'number', weak: true }
}

如果指定了 weak: true,那么 phone: "123456" 也被判定为合法

flowingfate avatar Aug 28 '17 11:08 flowingfate

非常支持,由于数组类型被额定之后,对于未知的类型,需要将type 作为一个数组,满足其一则条件成立。

veaba avatar Aug 30 '17 13:08 veaba

是否支持 type: ['string', 'number']?

fritx avatar Sep 04 '17 11:09 fritx

目前不支持该功能,我说的支持是支持楼主的说法。 @fritx

veaba avatar Sep 05 '17 01:09 veaba

哦,因为我在使用element-ui,基于async-validator,有些表单控件比如select,radio-group等,value可能是string或number。

fritx avatar Sep 05 '17 12:09 fritx

非常赞同这个,遇到了最严重问题就是弱类型

zhangyaogang avatar Sep 07 '17 12:09 zhangyaogang

不为空的时候希望不用写类型

zhangyaogang avatar Sep 07 '17 12:09 zhangyaogang

支持增加弱类型以及不指定类型校验

ZhanYishu avatar Nov 05 '17 09:11 ZhanYishu

我有个问题,怎么采用多规则,比如我又个字符,检测字符中包含有链接就能通过,或者是提供8个数字+字母也可以通过

这个或者条件怎么加?

cgfeel avatar Aug 12 '19 02:08 cgfeel

@aiyuchen 可以用自定义校验规则来做嘛

Riant avatar Aug 12 '19 04:08 Riant

addresses: {
    type: "array", required: true,
    defaultField: {
      street: {type: "string", required: true},
      city: {type: "string", required: true},
      zip: {type: "string", required: true, len: 8, message: "invalid zip"}
    }
  }

当 addresses 是一个数组的时候,不知道这种配置支持不。

Riant avatar Aug 12 '19 04:08 Riant