多类型验证 + 弱类型兼容
- 如果希望数组中的值全都采用同样的规则来校验,对此,能否提供这样的支持,例如:
rules = {
names: { type: 'array', arrayItems: { type: 'string' } }
}
这样来指定,所有数组的值都只能是字符串
- API 调用中经常遇到这种情况:传参时可以传
字符串也可以传字符串数组,例如:
// 单入参
abridge.showActionSheet({
items: '菜单名',
})
// 数组入参
abridge.showActionSheet({
items: ['菜单1', '菜单2', '菜单3', '菜单4'],
})
对此,能否提供多类型校验功能,例如:
rules = {
items: { type: ['string', 'array'], arrayItems: { type: 'string' } }
}
如果 type 指定为数组,则 items 满足其一都可以
- 数字存在弱类型判断问题,现有的逻辑中,如果声明了
type: 'number',"1234"会被判定为不合法,然而在使用过程中,这种情况很难避免,对此,能否提供一个弱类型支持,例如:
rules = {
phone: { type: 'number', weak: true }
}
如果指定了 weak: true,那么 phone: "123456" 也被判定为合法
非常支持,由于数组类型被额定之后,对于未知的类型,需要将type 作为一个数组,满足其一则条件成立。
是否支持 type: ['string', 'number']?
目前不支持该功能,我说的支持是支持楼主的说法。 @fritx
哦,因为我在使用element-ui,基于async-validator,有些表单控件比如select,radio-group等,value可能是string或number。
非常赞同这个,遇到了最严重问题就是弱类型
不为空的时候希望不用写类型
支持增加弱类型以及不指定类型校验
我有个问题,怎么采用多规则,比如我又个字符,检测字符中包含有链接就能通过,或者是提供8个数字+字母也可以通过
这个或者条件怎么加?
@aiyuchen 可以用自定义校验规则来做嘛
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 是一个数组的时候,不知道这种配置支持不。