rxtutils 1.1.4-beta.9 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -49
- package/cjs/_utils/defaultEquals.cjs +4 -19
- package/cjs/cache/index.cjs +130 -163
- package/cjs/cache/indexDB.cjs +86 -99
- package/cjs/downloadBlob.cjs +14 -0
- package/cjs/hooks/index.cjs +3 -6
- package/cjs/hooks/useCombineControlValue.cjs +33 -41
- package/cjs/index.cjs +16 -17
- package/cjs/request/defaultHandlers.cjs +5 -27
- package/cjs/request/error.cjs +18 -30
- package/cjs/request/index.cjs +194 -174
- package/cjs/store/createGetter/index.cjs +26 -40
- package/cjs/store/createStateStore/index.cjs +47 -90
- package/cjs/store/index.cjs +4 -7
- package/cjs/validator/decorators.cjs +71 -228
- package/cjs/validator/index.cjs +4 -7
- package/cjs/validator/validator.cjs +101 -177
- package/es/_utils/defaultEquals.mjs +4 -17
- package/es/cache/index.d.ts +9 -13
- package/es/cache/index.mjs +132 -160
- package/es/cache/indexDB.d.ts +1 -3
- package/es/cache/indexDB.mjs +87 -98
- package/es/downloadBlob.d.ts +8 -0
- package/es/downloadBlob.mjs +14 -0
- package/es/hooks/index.d.ts +1 -1
- package/es/hooks/index.mjs +4 -1
- package/es/hooks/useCombineControlValue.d.ts +5 -8
- package/es/hooks/useCombineControlValue.mjs +34 -40
- package/es/index.d.ts +28 -8
- package/es/index.mjs +29 -7
- package/es/request/defaultHandlers.d.ts +24 -0
- package/es/request/defaultHandlers.mjs +8 -26
- package/es/request/error.d.ts +3 -6
- package/es/request/error.mjs +18 -28
- package/es/request/index.d.ts +32 -20
- package/es/request/index.mjs +194 -172
- package/es/store/createGetter/index.d.ts +6 -10
- package/es/store/createGetter/index.mjs +28 -39
- package/es/store/createStateStore/index.d.ts +9 -9
- package/es/store/createStateStore/index.mjs +49 -87
- package/es/store/index.d.ts +4 -2
- package/es/store/index.mjs +7 -2
- package/es/validator/decorators.d.ts +12 -21
- package/es/validator/decorators.mjs +81 -226
- package/es/validator/index.d.ts +2 -2
- package/es/validator/index.mjs +16 -2
- package/es/validator/validator.d.ts +5 -6
- package/es/validator/validator.mjs +102 -176
- package/package.json +85 -15
- package/cjs/_utils/deepAssign.cjs +0 -25
- package/cjs/cache/index.d.ts +0 -141
- package/cjs/cache/indexDB.d.ts +0 -52
- package/cjs/hooks/index.d.ts +0 -1
- package/cjs/hooks/useCombineControlValue.d.ts +0 -21
- package/cjs/index.d.ts +0 -8
- package/cjs/request/error.d.ts +0 -31
- package/cjs/request/index.d.ts +0 -147
- package/cjs/store/createGetter/index.d.ts +0 -30
- package/cjs/store/createStateStore/index.d.ts +0 -42
- package/cjs/store/index.d.ts +0 -2
- package/cjs/validator/decorators.d.ts +0 -159
- package/cjs/validator/index.d.ts +0 -2
- package/cjs/validator/validator.d.ts +0 -84
- package/es/_utils/deepAssign.mjs +0 -23
|
@@ -1,238 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* 这些装饰器基于 BaseValidator 的 decoratorCreator 方法创建
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* 必填项验证装饰器
|
|
12
|
-
* 验证值是否存在且不在指定的无效值列表中
|
|
13
|
-
*
|
|
14
|
-
* @param noneVals 被视为无效的值数组,默认为 [undefined]
|
|
15
|
-
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* class User extends BaseValidator {
|
|
19
|
-
* @(VRequired()('用户名不能为空'))
|
|
20
|
-
* username?: string;
|
|
21
|
-
* }
|
|
22
|
-
*/
|
|
23
|
-
function VRequired(noneVals) {
|
|
24
|
-
if (noneVals === void 0) { noneVals = [undefined]; }
|
|
25
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
26
|
-
if (noneVals.includes(val)) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
return true;
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* 字符串类型验证装饰器
|
|
34
|
-
* 验证值是否为字符串类型
|
|
35
|
-
*
|
|
36
|
-
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* class User extends BaseValidator {
|
|
40
|
-
* @VString('用户名必须为字符串')
|
|
41
|
-
* username?: string;
|
|
42
|
-
* }
|
|
43
|
-
*/
|
|
44
|
-
var VString = validator.BaseValidator.decoratorCreator(function (val) {
|
|
45
|
-
if (typeof val !== 'string') {
|
|
46
|
-
return false;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const validator = require("./validator.cjs");
|
|
4
|
+
function VRequired(noneVals = [void 0]) {
|
|
5
|
+
return validator.BaseValidator.decoratorCreator((val) => {
|
|
6
|
+
if (noneVals.includes(val)) {
|
|
7
|
+
return false;
|
|
47
8
|
}
|
|
48
9
|
return true;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
const VString = validator.BaseValidator.decoratorCreator((val) => {
|
|
13
|
+
if (typeof val !== "string") {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
49
17
|
});
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* class User extends BaseValidator {
|
|
58
|
-
* @VNumber('年龄必须为数字')
|
|
59
|
-
* age?: number;
|
|
60
|
-
* }
|
|
61
|
-
*/
|
|
62
|
-
var VNumber = validator.BaseValidator.decoratorCreator(function (val) {
|
|
63
|
-
if (typeof val !== 'number') {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
return true;
|
|
18
|
+
const VNumber = validator.BaseValidator.decoratorCreator((val) => {
|
|
19
|
+
if (typeof val !== "number") {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
67
23
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* class User extends BaseValidator {
|
|
76
|
-
* @VArray('标签必须为数组')
|
|
77
|
-
* tags?: string[];
|
|
78
|
-
* }
|
|
79
|
-
*/
|
|
80
|
-
var VArray = validator.BaseValidator.decoratorCreator(function (val) {
|
|
81
|
-
if (!Array.isArray(val)) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
return true;
|
|
24
|
+
const VArray = validator.BaseValidator.decoratorCreator((val) => {
|
|
25
|
+
if (!Array.isArray(val)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
85
29
|
});
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* class User extends BaseValidator {
|
|
94
|
-
* @VBoolean('状态必须为布尔值')
|
|
95
|
-
* active?: boolean;
|
|
96
|
-
* }
|
|
97
|
-
*/
|
|
98
|
-
var VBoolean = validator.BaseValidator.decoratorCreator(function (val) {
|
|
99
|
-
if (typeof val !== 'boolean') {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
return true;
|
|
30
|
+
const VBoolean = validator.BaseValidator.decoratorCreator((val) => {
|
|
31
|
+
if (typeof val !== "boolean") {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
103
35
|
});
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* 最小长度验证装饰器
|
|
148
|
-
* 验证字符串或数组的长度是否大于或等于指定的最小长度
|
|
149
|
-
*
|
|
150
|
-
* @param minLen 最小长度
|
|
151
|
-
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* class User extends BaseValidator {
|
|
155
|
-
* @(VMinLength(6)('密码长度不能少于6位'))
|
|
156
|
-
* password?: string;
|
|
157
|
-
* }
|
|
158
|
-
*/
|
|
159
|
-
var VMinLength = function (minLen) {
|
|
160
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
161
|
-
if (typeof val !== 'string' && !Array.isArray(val)) {
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
if (val.length < minLen) {
|
|
165
|
-
return false;
|
|
166
|
-
}
|
|
167
|
-
return true;
|
|
168
|
-
});
|
|
169
|
-
};
|
|
170
|
-
/**
|
|
171
|
-
* 最大长度验证装饰器
|
|
172
|
-
* 验证字符串或数组的长度是否小于或等于指定的最大长度
|
|
173
|
-
*
|
|
174
|
-
* @param maxLen 最大长度
|
|
175
|
-
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
* class User extends BaseValidator {
|
|
179
|
-
* @(VMaxLength(20)('用户名长度不能超过20位'))
|
|
180
|
-
* username?: string;
|
|
181
|
-
* }
|
|
182
|
-
*/
|
|
183
|
-
var VMaxLength = function (maxLen) {
|
|
184
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
185
|
-
if (typeof val !== 'string' && !Array.isArray(val)) {
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
if (val.length > maxLen) {
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
return true;
|
|
192
|
-
});
|
|
193
|
-
};
|
|
194
|
-
/**
|
|
195
|
-
* 邮箱格式验证装饰器
|
|
196
|
-
* 验证字符串是否符合邮箱格式
|
|
197
|
-
*
|
|
198
|
-
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
199
|
-
*
|
|
200
|
-
* @example
|
|
201
|
-
* class User extends BaseValidator {
|
|
202
|
-
* @VEmail('邮箱格式不正确')
|
|
203
|
-
* email?: string;
|
|
204
|
-
* }
|
|
205
|
-
*/
|
|
206
|
-
var VEmail = validator.BaseValidator.decoratorCreator(function (val) {
|
|
207
|
-
if (typeof val !== 'string') {
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
// 简单邮箱正则
|
|
211
|
-
var emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
212
|
-
return emailReg.test(val);
|
|
36
|
+
const VMin = (min) => validator.BaseValidator.decoratorCreator((val) => {
|
|
37
|
+
if (typeof val !== "number" || val < min) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
});
|
|
42
|
+
const VMax = (max) => validator.BaseValidator.decoratorCreator((val) => {
|
|
43
|
+
if (typeof val !== "number" || val > max) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
});
|
|
48
|
+
const VMinLength = (minLen) => validator.BaseValidator.decoratorCreator((val) => {
|
|
49
|
+
if (typeof val !== "string" && !Array.isArray(val)) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if (val.length < minLen) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
});
|
|
57
|
+
const VMaxLength = (maxLen) => validator.BaseValidator.decoratorCreator((val) => {
|
|
58
|
+
if (typeof val !== "string" && !Array.isArray(val)) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (val.length > maxLen) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
});
|
|
66
|
+
const VEmail = validator.BaseValidator.decoratorCreator((val) => {
|
|
67
|
+
if (typeof val !== "string") {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
const emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
71
|
+
return emailReg.test(val);
|
|
72
|
+
});
|
|
73
|
+
const VPattern = (pattern) => validator.BaseValidator.decoratorCreator((val) => {
|
|
74
|
+
if (typeof val !== "string") {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return pattern.test(val);
|
|
213
78
|
});
|
|
214
|
-
/**
|
|
215
|
-
* 正则表达式验证装饰器
|
|
216
|
-
* 验证字符串是否匹配指定的正则表达式模式
|
|
217
|
-
*
|
|
218
|
-
* @param pattern 正则表达式
|
|
219
|
-
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
220
|
-
*
|
|
221
|
-
* @example
|
|
222
|
-
* class User extends BaseValidator {
|
|
223
|
-
* @(VPattern(/^1[3-9]\d{9}$/)('手机号格式不正确'))
|
|
224
|
-
* phone?: string;
|
|
225
|
-
* }
|
|
226
|
-
*/
|
|
227
|
-
var VPattern = function (pattern) {
|
|
228
|
-
return validator.BaseValidator.decoratorCreator(function (val) {
|
|
229
|
-
if (typeof val !== 'string') {
|
|
230
|
-
return false;
|
|
231
|
-
}
|
|
232
|
-
return pattern.test(val);
|
|
233
|
-
});
|
|
234
|
-
};
|
|
235
|
-
|
|
236
79
|
exports.VArray = VArray;
|
|
237
80
|
exports.VBoolean = VBoolean;
|
|
238
81
|
exports.VEmail = VEmail;
|
package/cjs/validator/index.cjs
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const validator = require("./validator.cjs");
|
|
4
|
+
const decorators = require("./decorators.cjs");
|
|
8
5
|
exports.BaseValidator = validator.BaseValidator;
|
|
9
6
|
exports.VArray = decorators.VArray;
|
|
10
7
|
exports.VBoolean = decorators.VBoolean;
|
|
@@ -1,184 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const KEY_SYMBOL = /* @__PURE__ */ Symbol("key-description");
|
|
4
|
+
const _BaseValidator = class _BaseValidator {
|
|
5
|
+
/**
|
|
6
|
+
* 构造函数
|
|
7
|
+
* 初始化验证器映射存储
|
|
8
|
+
*/
|
|
9
|
+
constructor() {
|
|
10
|
+
this.__keySymbol = KEY_SYMBOL;
|
|
11
|
+
this[this.__keySymbol] = {};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 验证单个字段
|
|
15
|
+
* @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
16
|
+
* @param itemKey 要验证的字段名
|
|
17
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
18
|
+
*/
|
|
19
|
+
validate(itemKey, itemAll = false) {
|
|
20
|
+
const validatorMap = this[this.__keySymbol];
|
|
21
|
+
const errors = [];
|
|
22
|
+
const validators = validatorMap[itemKey];
|
|
23
|
+
if (!validators) {
|
|
24
|
+
return null;
|
|
19
25
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
26
|
+
for (const validator of validators) {
|
|
27
|
+
const res = validator(this[itemKey]);
|
|
28
|
+
if (!res.status) {
|
|
29
|
+
errors.push(res.message || `${itemKey} error`);
|
|
30
|
+
if (!itemAll) break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (errors.length) {
|
|
34
|
+
return errors;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 验证多个或所有字段
|
|
40
|
+
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序,默认验证所有字段,按对象定义顺序
|
|
41
|
+
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止,默认为 false
|
|
42
|
+
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止,默认为 false
|
|
43
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
44
|
+
*/
|
|
45
|
+
validateAll(order, itemAll = false, everyItem = false) {
|
|
46
|
+
const validatorMap = this[this.__keySymbol];
|
|
47
|
+
const errors = {};
|
|
48
|
+
const keys = order || Object.keys(validatorMap);
|
|
49
|
+
for (const key of keys) {
|
|
50
|
+
const value = this[key];
|
|
51
|
+
const fns = validatorMap[key];
|
|
52
|
+
for (const fn of fns) {
|
|
53
|
+
const res = fn(value);
|
|
54
|
+
if (!res.status) {
|
|
55
|
+
const msg = res.message || `${key} error`;
|
|
56
|
+
if (Array.isArray(errors[key])) {
|
|
57
|
+
errors[key].push(msg);
|
|
58
|
+
} else {
|
|
59
|
+
errors[key] = [msg];
|
|
60
|
+
}
|
|
61
|
+
if (!itemAll) break;
|
|
43
62
|
}
|
|
44
|
-
|
|
45
|
-
|
|
63
|
+
}
|
|
64
|
+
if (errors[key] && !everyItem) {
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (Object.keys(errors).length) {
|
|
69
|
+
return errors;
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
_BaseValidator.decoratorCreator = (func) => {
|
|
75
|
+
return (message = (val, value, context) => `${String(context.name)}格式错误`) => {
|
|
76
|
+
return function(value, context) {
|
|
77
|
+
context.addInitializer(function() {
|
|
78
|
+
let validators = this[this.__keySymbol];
|
|
79
|
+
if (!validators) {
|
|
80
|
+
this[this.__keySymbol] = {};
|
|
81
|
+
validators = this[this.__keySymbol];
|
|
46
82
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (everyItem === void 0) { everyItem = false; }
|
|
59
|
-
var validatorMap = this[this.__keySymbol];
|
|
60
|
-
var errors = {};
|
|
61
|
-
// 校验每个 key
|
|
62
|
-
var keys = order || Object.keys(validatorMap);
|
|
63
|
-
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
64
|
-
var key = keys_1[_i];
|
|
65
|
-
var value = this[key];
|
|
66
|
-
var fns = validatorMap[key];
|
|
67
|
-
// 校验每个校验项目
|
|
68
|
-
for (var _a = 0, fns_1 = fns; _a < fns_1.length; _a++) {
|
|
69
|
-
var fn = fns_1[_a];
|
|
70
|
-
var res = fn(value);
|
|
71
|
-
if (!res.status) {
|
|
72
|
-
if (Array.isArray(errors[res.name])) {
|
|
73
|
-
errors[res.name].push(res.message);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
errors[res.name] = [res.message];
|
|
77
|
-
}
|
|
78
|
-
if (!itemAll)
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
83
|
+
const name = context.name;
|
|
84
|
+
const validator = (val) => {
|
|
85
|
+
const validateStatus = func(val, value, context);
|
|
86
|
+
if (validateStatus) {
|
|
87
|
+
return { name, status: true };
|
|
88
|
+
} else {
|
|
89
|
+
let msg = "";
|
|
90
|
+
if (typeof message === "function") {
|
|
91
|
+
msg = message(val, value, context);
|
|
92
|
+
} else {
|
|
93
|
+
msg = message;
|
|
81
94
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
if (Object.keys(errors).length) {
|
|
87
|
-
return errors;
|
|
88
|
-
}
|
|
89
|
-
return null;
|
|
90
|
-
};
|
|
91
|
-
/**
|
|
92
|
-
* 装饰器创建器
|
|
93
|
-
* 用于创建属性验证装饰器的工厂函数
|
|
94
|
-
*
|
|
95
|
-
* @param func 验证函数,接收属性值并返回布尔值表示验证结果
|
|
96
|
-
* @returns 返回一个接收错误消息的函数,该函数再返回实际的装饰器
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* // 创建一个验证字符串的装饰器
|
|
100
|
-
* const VString = BaseValidator.decoratorCreator(
|
|
101
|
-
* (val) => typeof val === 'string' || val === undefined
|
|
102
|
-
* );
|
|
103
|
-
*
|
|
104
|
-
* // 创建一个验证必填项的装饰器
|
|
105
|
-
* const VRequired = BaseValidator.decoratorCreator(
|
|
106
|
-
* (val) => val !== undefined && val !== null && val !== ''
|
|
107
|
-
* );
|
|
108
|
-
*
|
|
109
|
-
* // 在类中使用这些装饰器
|
|
110
|
-
* class User extends BaseValidator {
|
|
111
|
-
* @VString('名称必须为字符串')
|
|
112
|
-
* @(VRequired()('名称必须填写'))
|
|
113
|
-
* name?: string;
|
|
114
|
-
*
|
|
115
|
-
* // 验证使用
|
|
116
|
-
* validateName() {
|
|
117
|
-
* return this.validate('name');
|
|
118
|
-
* }
|
|
119
|
-
* }
|
|
120
|
-
*
|
|
121
|
-
* const user = new User();
|
|
122
|
-
* console.log(user.validateName()); // 显示错误信息:名称必须填写
|
|
123
|
-
*/
|
|
124
|
-
BaseValidator.decoratorCreator = function (func) {
|
|
125
|
-
return function (message) {
|
|
126
|
-
if (message === void 0) { message = function (val, value, context) { return "".concat(String(context.name), "\u683C\u5F0F\u9519\u8BEF"); }; }
|
|
127
|
-
return function (value, context) {
|
|
128
|
-
context.addInitializer(function () {
|
|
129
|
-
var validators = this[this.__keySymbol];
|
|
130
|
-
if (!validators) {
|
|
131
|
-
this[this.__keySymbol] = {};
|
|
132
|
-
validators = this[this.__keySymbol];
|
|
133
|
-
}
|
|
134
|
-
var name = context.name;
|
|
135
|
-
var validator = function (val) {
|
|
136
|
-
var validateStatus = func(val, value, context);
|
|
137
|
-
if (validateStatus) {
|
|
138
|
-
return { name: name, status: true };
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
var msg = "";
|
|
142
|
-
if (typeof message === "function") {
|
|
143
|
-
msg = message(val, value, context);
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
msg = message;
|
|
147
|
-
}
|
|
148
|
-
return { name: name, status: false, message: msg };
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
if (validators[name]) {
|
|
152
|
-
validators[name] = tslib.__spreadArray(tslib.__spreadArray([], validators[name], true), [validator], false);
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
validators[name] = [validator];
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
};
|
|
95
|
+
return { name, status: false, message: msg };
|
|
96
|
+
}
|
|
159
97
|
};
|
|
98
|
+
if (validators[name]) {
|
|
99
|
+
validators[name] = [...validators[name], validator];
|
|
100
|
+
} else {
|
|
101
|
+
validators[name] = [validator];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
160
104
|
};
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// @VString('名称必须为字符串')
|
|
165
|
-
// @(VRequired()('名称必须填写'))
|
|
166
|
-
// name?: string;
|
|
167
|
-
// @VNumber('年龄必须为数字')
|
|
168
|
-
// @(VRequired()('年龄必须填写'))
|
|
169
|
-
// age?: number;
|
|
170
|
-
// @VEmail('邮箱格式不正确')
|
|
171
|
-
// @(VRequired()('邮箱必须填写'))
|
|
172
|
-
// email?: string;
|
|
173
|
-
// @(VMinLength(6)('密码长度不能少于6位'))
|
|
174
|
-
// @(VRequired()('密码必须填写'))
|
|
175
|
-
// password?: string
|
|
176
|
-
// }
|
|
177
|
-
// const user = new User();
|
|
178
|
-
// user.name = '张三'
|
|
179
|
-
// user.email = ' asdfasdf'
|
|
180
|
-
// user.password = '12345'
|
|
181
|
-
// console.log(user)
|
|
182
|
-
// console.log(user.validateAll(false,true,['password','age','email']));
|
|
183
|
-
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
let BaseValidator = _BaseValidator;
|
|
184
108
|
exports.BaseValidator = BaseValidator;
|
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 默认的相等性比较函数
|
|
3
|
-
* 通过将两个值序列化为 JSON 字符串来比较它们是否相等
|
|
4
|
-
*
|
|
5
|
-
* @template Param 比较值的类型,默认为 any
|
|
6
|
-
* @param prev 前一个值
|
|
7
|
-
* @param next 后一个值
|
|
8
|
-
* @returns {boolean} 如果两个值相等则返回 true,否则返回 false
|
|
9
|
-
*
|
|
10
|
-
* @remarks
|
|
11
|
-
* - 这个函数通过 JSON.stringify 进行比较,适用于大多数简单的数据结构
|
|
12
|
-
* - 不适用于包含函数、undefined、Symbol 等无法序列化的值
|
|
13
|
-
* - 对于循环引用的对象会抛出错误
|
|
14
|
-
*/
|
|
15
1
|
function defaultEquals(prev, next) {
|
|
16
|
-
|
|
2
|
+
return JSON.stringify(prev) === JSON.stringify(next);
|
|
17
3
|
}
|
|
18
|
-
|
|
19
|
-
|
|
4
|
+
export {
|
|
5
|
+
defaultEquals as default
|
|
6
|
+
};
|