fix:表单项值非string时,部分校验规则一直生效 (#11093)

Co-authored-by: hezhihang <hezhihang@baidu.com>
This commit is contained in:
Qnets 2024-10-22 20:20:01 +08:00 committed by GitHub
parent b93292c100
commit 7d84805605
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -91,6 +91,16 @@ const makeUrlRegexp = memoize(function (options: any) {
return new RegExp(regex, 'i');
});
const valueToString = (value: any) => {
return typeof value === 'undefined' || value === null
? ''
: typeof value === 'string'
? value
: value instanceof Date
? value.toISOString()
: JSON.stringify(value);
};
export interface ValidateFn {
(
values: {[propsName: string]: any},
@ -177,9 +187,7 @@ export const validations: {
},
isLength: function (values, value, length) {
// 此方法应该判断文本长度如果传入数据为number导致 maxLength 和 maximum 表现一致了默认转成string
if (typeof value === 'number') {
value = String(value);
}
value = valueToString(value);
return !isExisty(value) || isEmpty(value) || value.length === length;
},
@ -191,16 +199,14 @@ export const validations: {
},
maxLength: function (values, value, length) {
// 此方法应该判断文本长度如果传入数据为number导致 maxLength 和 maximum 表现一致了默认转成string
if (typeof value === 'number') {
value = String(value);
}
value = valueToString(value);
return !isExisty(value) || value.length <= length;
},
minLength: function (values, value, length) {
// 此方法应该判断文本长度如果传入数据为number导致 maxLength 和 maximum 表现一致了默认转成string
if (typeof value === 'number') {
value = String(value);
}
value = valueToString(value);
return !isExisty(value) || isEmpty(value) || value.length >= length;
},
isUrlPath: function (values, value, regexp) {