rxtutils 1.1.2-beta.2 → 1.1.2-beta.21

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.
Files changed (56) hide show
  1. package/README.md +144 -1
  2. package/{dist/cjs → cjs}/cache/index.cjs +28 -15
  3. package/{dist/types → cjs}/cache/index.d.ts +13 -7
  4. package/cjs/request/index.cjs +150 -0
  5. package/{dist/types → cjs}/request/index.d.ts +12 -8
  6. package/cjs/store/index.cjs +10 -0
  7. package/cjs/store/index.d.ts +2 -0
  8. package/cjs/validator/decorators.cjs +246 -0
  9. package/cjs/validator/decorators.d.ts +159 -0
  10. package/cjs/validator/index.cjs +19 -0
  11. package/cjs/validator/index.d.ts +2 -0
  12. package/{dist/cjs → cjs}/validator/validator.cjs +78 -13
  13. package/cjs/validator/validator.d.ts +84 -0
  14. package/es/cache/index.d.ts +141 -0
  15. package/{dist/es → es}/cache/index.mjs +28 -15
  16. package/es/cache/indexDB.d.ts +52 -0
  17. package/es/index.d.ts +7 -0
  18. package/es/request/error.d.ts +31 -0
  19. package/es/request/index.d.ts +140 -0
  20. package/es/request/index.mjs +148 -0
  21. package/es/store/createGetter/index.d.ts +30 -0
  22. package/es/store/createStateStore/index.d.ts +42 -0
  23. package/es/store/index.d.ts +2 -0
  24. package/es/store/index.mjs +2 -0
  25. package/es/validator/decorators.d.ts +159 -0
  26. package/es/validator/decorators.mjs +234 -0
  27. package/es/validator/index.d.ts +2 -0
  28. package/es/validator/index.mjs +2 -0
  29. package/es/validator/validator.d.ts +84 -0
  30. package/{dist/es → es}/validator/validator.mjs +78 -13
  31. package/package.json +12 -9
  32. package/dist/cjs/request/index.cjs +0 -129
  33. package/dist/cjs/validator/decorators.cjs +0 -104
  34. package/dist/es/request/index.mjs +0 -127
  35. package/dist/es/validator/decorators.mjs +0 -92
  36. package/dist/types/validator/decorators.d.ts +0 -15
  37. package/dist/types/validator/validator.d.ts +0 -24
  38. /package/{dist/cjs → cjs/_utils}/defaultEquals.cjs +0 -0
  39. /package/{dist/cjs → cjs}/cache/indexDB.cjs +0 -0
  40. /package/{dist/types → cjs}/cache/indexDB.d.ts +0 -0
  41. /package/{dist/cjs → cjs}/index.cjs +0 -0
  42. /package/{dist/types → cjs}/index.d.ts +0 -0
  43. /package/{dist/cjs → cjs}/request/defaultHandlers.cjs +0 -0
  44. /package/{dist/cjs → cjs}/request/error.cjs +0 -0
  45. /package/{dist/types → cjs}/request/error.d.ts +0 -0
  46. /package/{dist/cjs → cjs}/store/createGetter/index.cjs +0 -0
  47. /package/{dist/types → cjs}/store/createGetter/index.d.ts +0 -0
  48. /package/{dist/cjs → cjs}/store/createStateStore/index.cjs +0 -0
  49. /package/{dist/types → cjs}/store/createStateStore/index.d.ts +0 -0
  50. /package/{dist/es → es/_utils}/defaultEquals.mjs +0 -0
  51. /package/{dist/es → es}/cache/indexDB.mjs +0 -0
  52. /package/{dist/es → es}/index.mjs +0 -0
  53. /package/{dist/es → es}/request/defaultHandlers.mjs +0 -0
  54. /package/{dist/es → es}/request/error.mjs +0 -0
  55. /package/{dist/es → es}/store/createGetter/index.mjs +0 -0
  56. /package/{dist/es → es}/store/createStateStore/index.mjs +0 -0
@@ -0,0 +1,246 @@
1
+ 'use strict';
2
+
3
+ var validator = require('./validator.cjs');
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;
47
+ }
48
+ return true;
49
+ });
50
+ /**
51
+ * 数字类型验证装饰器
52
+ * 验证值是否为数字类型
53
+ *
54
+ * @returns 装饰器工厂函数,可接收自定义错误消息
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;
67
+ });
68
+ /**
69
+ * 数组类型验证装饰器
70
+ * 验证值是否为数组类型
71
+ *
72
+ * @returns 装饰器工厂函数,可接收自定义错误消息
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;
85
+ });
86
+ /**
87
+ * 布尔类型验证装饰器
88
+ * 验证值是否为布尔类型
89
+ *
90
+ * @returns 装饰器工厂函数,可接收自定义错误消息
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;
103
+ });
104
+ /**
105
+ * 最小值验证装饰器
106
+ * 验证数字是否大于或等于指定的最小值
107
+ *
108
+ * @param min 最小值
109
+ * @returns 装饰器工厂函数,可接收自定义错误消息
110
+ *
111
+ * @example
112
+ * class User extends BaseValidator {
113
+ * @(VMin(18)('年龄必须大于或等于18岁'))
114
+ * age?: number;
115
+ * }
116
+ */
117
+ var VMin = function (min) {
118
+ return validator.BaseValidator.decoratorCreator(function (val) {
119
+ if (typeof val !== 'number' || val < min) {
120
+ return false;
121
+ }
122
+ return true;
123
+ });
124
+ };
125
+ /**
126
+ * 最大值验证装饰器
127
+ * 验证数字是否小于或等于指定的最大值
128
+ *
129
+ * @param max 最大值
130
+ * @returns 装饰器工厂函数,可接收自定义错误消息
131
+ *
132
+ * @example
133
+ * class User extends BaseValidator {
134
+ * @(VMax(120)('年龄必须小于或等于120岁'))
135
+ * age?: number;
136
+ * }
137
+ */
138
+ var VMax = function (max) {
139
+ return validator.BaseValidator.decoratorCreator(function (val) {
140
+ if (typeof val !== 'number' || val > max) {
141
+ return false;
142
+ }
143
+ return true;
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);
213
+ });
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
+ exports.VArray = VArray;
237
+ exports.VBoolean = VBoolean;
238
+ exports.VEmail = VEmail;
239
+ exports.VMax = VMax;
240
+ exports.VMaxLength = VMaxLength;
241
+ exports.VMin = VMin;
242
+ exports.VMinLength = VMinLength;
243
+ exports.VNumber = VNumber;
244
+ exports.VPattern = VPattern;
245
+ exports.VRequired = VRequired;
246
+ exports.VString = VString;
@@ -0,0 +1,159 @@
1
+ import { BaseValidator } from './validator.js';
2
+
3
+ /**
4
+ * 验证器装饰器模块
5
+ * 提供一系列用于数据验证的装饰器,可用于类属性的验证规则定义
6
+ * 这些装饰器基于 BaseValidator 的 decoratorCreator 方法创建
7
+ */
8
+
9
+ /**
10
+ * 必填项验证装饰器
11
+ * 验证值是否存在且不在指定的无效值列表中
12
+ *
13
+ * @param noneVals 被视为无效的值数组,默认为 [undefined]
14
+ * @returns 装饰器工厂函数,可接收自定义错误消息
15
+ *
16
+ * @example
17
+ * class User extends BaseValidator {
18
+ * @(VRequired()('用户名不能为空'))
19
+ * username?: string;
20
+ * }
21
+ */
22
+ declare function VRequired(noneVals?: any[]): (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
23
+ /**
24
+ * 字符串类型验证装饰器
25
+ * 验证值是否为字符串类型
26
+ *
27
+ * @returns 装饰器工厂函数,可接收自定义错误消息
28
+ *
29
+ * @example
30
+ * class User extends BaseValidator {
31
+ * @VString('用户名必须为字符串')
32
+ * username?: string;
33
+ * }
34
+ */
35
+ declare const VString: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
36
+ /**
37
+ * 数字类型验证装饰器
38
+ * 验证值是否为数字类型
39
+ *
40
+ * @returns 装饰器工厂函数,可接收自定义错误消息
41
+ *
42
+ * @example
43
+ * class User extends BaseValidator {
44
+ * @VNumber('年龄必须为数字')
45
+ * age?: number;
46
+ * }
47
+ */
48
+ declare const VNumber: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
49
+ /**
50
+ * 数组类型验证装饰器
51
+ * 验证值是否为数组类型
52
+ *
53
+ * @returns 装饰器工厂函数,可接收自定义错误消息
54
+ *
55
+ * @example
56
+ * class User extends BaseValidator {
57
+ * @VArray('标签必须为数组')
58
+ * tags?: string[];
59
+ * }
60
+ */
61
+ declare const VArray: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
62
+ /**
63
+ * 布尔类型验证装饰器
64
+ * 验证值是否为布尔类型
65
+ *
66
+ * @returns 装饰器工厂函数,可接收自定义错误消息
67
+ *
68
+ * @example
69
+ * class User extends BaseValidator {
70
+ * @VBoolean('状态必须为布尔值')
71
+ * active?: boolean;
72
+ * }
73
+ */
74
+ declare const VBoolean: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
75
+ /**
76
+ * 最小值验证装饰器
77
+ * 验证数字是否大于或等于指定的最小值
78
+ *
79
+ * @param min 最小值
80
+ * @returns 装饰器工厂函数,可接收自定义错误消息
81
+ *
82
+ * @example
83
+ * class User extends BaseValidator {
84
+ * @(VMin(18)('年龄必须大于或等于18岁'))
85
+ * age?: number;
86
+ * }
87
+ */
88
+ declare const VMin: (min: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
89
+ /**
90
+ * 最大值验证装饰器
91
+ * 验证数字是否小于或等于指定的最大值
92
+ *
93
+ * @param max 最大值
94
+ * @returns 装饰器工厂函数,可接收自定义错误消息
95
+ *
96
+ * @example
97
+ * class User extends BaseValidator {
98
+ * @(VMax(120)('年龄必须小于或等于120岁'))
99
+ * age?: number;
100
+ * }
101
+ */
102
+ declare const VMax: (max: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
103
+ /**
104
+ * 最小长度验证装饰器
105
+ * 验证字符串或数组的长度是否大于或等于指定的最小长度
106
+ *
107
+ * @param minLen 最小长度
108
+ * @returns 装饰器工厂函数,可接收自定义错误消息
109
+ *
110
+ * @example
111
+ * class User extends BaseValidator {
112
+ * @(VMinLength(6)('密码长度不能少于6位'))
113
+ * password?: string;
114
+ * }
115
+ */
116
+ declare const VMinLength: (minLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
117
+ /**
118
+ * 最大长度验证装饰器
119
+ * 验证字符串或数组的长度是否小于或等于指定的最大长度
120
+ *
121
+ * @param maxLen 最大长度
122
+ * @returns 装饰器工厂函数,可接收自定义错误消息
123
+ *
124
+ * @example
125
+ * class User extends BaseValidator {
126
+ * @(VMaxLength(20)('用户名长度不能超过20位'))
127
+ * username?: string;
128
+ * }
129
+ */
130
+ declare const VMaxLength: (maxLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
131
+ /**
132
+ * 邮箱格式验证装饰器
133
+ * 验证字符串是否符合邮箱格式
134
+ *
135
+ * @returns 装饰器工厂函数,可接收自定义错误消息
136
+ *
137
+ * @example
138
+ * class User extends BaseValidator {
139
+ * @VEmail('邮箱格式不正确')
140
+ * email?: string;
141
+ * }
142
+ */
143
+ declare const VEmail: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
144
+ /**
145
+ * 正则表达式验证装饰器
146
+ * 验证字符串是否匹配指定的正则表达式模式
147
+ *
148
+ * @param pattern 正则表达式
149
+ * @returns 装饰器工厂函数,可接收自定义错误消息
150
+ *
151
+ * @example
152
+ * class User extends BaseValidator {
153
+ * @(VPattern(/^1[3-9]\d{9}$/)('手机号格式不正确'))
154
+ * phone?: string;
155
+ * }
156
+ */
157
+ declare const VPattern: (pattern: RegExp) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
158
+
159
+ export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString };
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ var validator = require('./validator.cjs');
4
+ var decorators = require('./decorators.cjs');
5
+
6
+
7
+
8
+ exports.BaseValidator = validator.BaseValidator;
9
+ exports.VArray = decorators.VArray;
10
+ exports.VBoolean = decorators.VBoolean;
11
+ exports.VEmail = decorators.VEmail;
12
+ exports.VMax = decorators.VMax;
13
+ exports.VMaxLength = decorators.VMaxLength;
14
+ exports.VMin = decorators.VMin;
15
+ exports.VMinLength = decorators.VMinLength;
16
+ exports.VNumber = decorators.VNumber;
17
+ exports.VPattern = decorators.VPattern;
18
+ exports.VRequired = decorators.VRequired;
19
+ exports.VString = decorators.VString;
@@ -0,0 +1,2 @@
1
+ export { BaseValidator } from './validator.js';
2
+ export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './decorators.js';
@@ -2,16 +2,31 @@
2
2
 
3
3
  var tslib = require('tslib');
4
4
 
5
+ /**
6
+ * 基础验证器类
7
+ * 提供字段验证功能,可通过装饰器为类属性添加验证规则
8
+ */
5
9
  var BaseValidator = /** @class */ (function () {
10
+ /**
11
+ * 构造函数
12
+ * 初始化验证器映射存储
13
+ */
6
14
  function BaseValidator() {
7
- this.__keySymbol = Symbol('key-description');
8
- this.__keySymbol = Symbol('key-description');
15
+ /** 用于存储验证器映射的私有符号 */
16
+ this.__keySymbol = Symbol("key-description");
17
+ this.__keySymbol = Symbol("key-description");
9
18
  this[this.__keySymbol] = {};
10
19
  }
20
+ /**
21
+ * 验证单个字段
22
+ * @param itemKey 要验证的字段名
23
+ * @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
24
+ * @returns 验证错误数组,如果没有错误则返回null
25
+ */
11
26
  BaseValidator.prototype.validate = function (itemKey, itemAll) {
12
27
  if (itemAll === void 0) { itemAll = false; }
13
28
  var validatorMap = this[this.__keySymbol];
14
- var errors = [];
29
+ var errors = {};
15
30
  // 校验每个 key
16
31
  var validators = validatorMap[itemKey];
17
32
  if (!validators) {
@@ -21,21 +36,33 @@ var BaseValidator = /** @class */ (function () {
21
36
  var validator = validators_1[_i];
22
37
  var res = validator(this[itemKey]);
23
38
  if (!res.status) {
24
- errors.push(res);
39
+ if (Array.isArray(errors[res.name])) {
40
+ errors[res.name].push(res.message);
41
+ }
42
+ else {
43
+ errors[res.name] = [res.message];
44
+ }
25
45
  if (!itemAll)
26
46
  break;
27
47
  }
28
48
  }
29
- if (errors.length) {
49
+ if (Object.keys(errors).length) {
30
50
  return errors;
31
51
  }
32
52
  return null;
33
53
  };
34
- BaseValidator.prototype.validateAll = function (itemAll, everyItem, order) {
54
+ /**
55
+ * 验证多个或所有字段
56
+ * @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
57
+ * @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
58
+ * @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
59
+ * @returns 验证错误数组,如果没有错误则返回null
60
+ */
61
+ BaseValidator.prototype.validateAll = function (order, itemAll, everyItem) {
35
62
  if (itemAll === void 0) { itemAll = false; }
36
63
  if (everyItem === void 0) { everyItem = false; }
37
64
  var validatorMap = this[this.__keySymbol];
38
- var errors = [];
65
+ var errors = {};
39
66
  // 校验每个 key
40
67
  var keys = order || Object.keys(validatorMap);
41
68
  for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
@@ -47,7 +74,12 @@ var BaseValidator = /** @class */ (function () {
47
74
  var fn = fns_1[_a];
48
75
  var res = fn(value);
49
76
  if (!res.status) {
50
- errors.push(res);
77
+ if (Array.isArray(errors[res.name])) {
78
+ errors[res.name].push(res.message);
79
+ }
80
+ else {
81
+ errors[res.name] = [res.message];
82
+ }
51
83
  if (!itemAll)
52
84
  break;
53
85
  }
@@ -56,11 +88,44 @@ var BaseValidator = /** @class */ (function () {
56
88
  break;
57
89
  }
58
90
  }
59
- if (errors.length) {
91
+ if (Object.keys(errors).length) {
60
92
  return errors;
61
93
  }
62
94
  return null;
63
95
  };
96
+ /**
97
+ * 装饰器创建器
98
+ * 用于创建属性验证装饰器的工厂函数
99
+ *
100
+ * @param func 验证函数,接收属性值并返回布尔值表示验证结果
101
+ * @returns 返回一个接收错误消息的函数,该函数再返回实际的装饰器
102
+ *
103
+ * @example
104
+ * // 创建一个验证字符串的装饰器
105
+ * const VString = BaseValidator.decoratorCreator(
106
+ * (val) => typeof val === 'string' || val === undefined
107
+ * );
108
+ *
109
+ * // 创建一个验证必填项的装饰器
110
+ * const VRequired = BaseValidator.decoratorCreator(
111
+ * (val) => val !== undefined && val !== null && val !== ''
112
+ * );
113
+ *
114
+ * // 在类中使用这些装饰器
115
+ * class User extends BaseValidator {
116
+ * @VString('名称必须为字符串')
117
+ * @(VRequired()('名称必须填写'))
118
+ * name?: string;
119
+ *
120
+ * // 验证使用
121
+ * validateName() {
122
+ * return this.validate('name');
123
+ * }
124
+ * }
125
+ *
126
+ * const user = new User();
127
+ * console.log(user.validateName()); // 显示错误信息:名称必须填写
128
+ */
64
129
  BaseValidator.decoratorCreator = function (func) {
65
130
  return function (message) {
66
131
  if (message === void 0) { message = function (val, value, context) { return "".concat(String(context.name), "\u683C\u5F0F\u9519\u8BEF"); }; }
@@ -75,17 +140,17 @@ var BaseValidator = /** @class */ (function () {
75
140
  var validator = function (val) {
76
141
  var validateStatus = func(val, value, context);
77
142
  if (validateStatus) {
78
- return { status: true };
143
+ return { name: name, status: true };
79
144
  }
80
145
  else {
81
- var msg = '';
82
- if (typeof message === 'function') {
146
+ var msg = "";
147
+ if (typeof message === "function") {
83
148
  msg = message(val, value, context);
84
149
  }
85
150
  else {
86
151
  msg = message;
87
152
  }
88
- return { status: false, message: msg };
153
+ return { name: name, status: false, message: msg };
89
154
  }
90
155
  };
91
156
  if (validators[name]) {
@@ -0,0 +1,84 @@
1
+ /**
2
+ * 验证器函数类型,接收任意值并返回验证结果
3
+ * @param val 需要验证的值
4
+ * @returns 包含验证状态和可选错误消息的对象
5
+ */
6
+ type Validator = (val: any) => {
7
+ name: string;
8
+ status: boolean;
9
+ message?: string;
10
+ };
11
+ /**
12
+ * 验证器映射类型,用于存储字段名到验证器数组的映射
13
+ */
14
+ type ValidatorMap = {
15
+ [key: string]: Validator[];
16
+ };
17
+ /**
18
+ * 基础验证器类
19
+ * 提供字段验证功能,可通过装饰器为类属性添加验证规则
20
+ */
21
+ declare class BaseValidator {
22
+ /** 用于存储验证器映射的私有符号 */
23
+ private __keySymbol;
24
+ /** 用于存储验证器映射的索引签名 */
25
+ [key: symbol]: ValidatorMap;
26
+ /** 用于存储类属性的索引签名 */
27
+ [key: string]: any;
28
+ /**
29
+ * 构造函数
30
+ * 初始化验证器映射存储
31
+ */
32
+ constructor();
33
+ /**
34
+ * 验证单个字段
35
+ * @param itemKey 要验证的字段名
36
+ * @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
37
+ * @returns 验证错误数组,如果没有错误则返回null
38
+ */
39
+ validate(itemKey: string, itemAll?: boolean): Record<string, string[]>;
40
+ /**
41
+ * 验证多个或所有字段
42
+ * @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
43
+ * @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
44
+ * @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
45
+ * @returns 验证错误数组,如果没有错误则返回null
46
+ */
47
+ validateAll(order?: string[], itemAll?: boolean, everyItem?: boolean): Record<string, string[]>;
48
+ /**
49
+ * 装饰器创建器
50
+ * 用于创建属性验证装饰器的工厂函数
51
+ *
52
+ * @param func 验证函数,接收属性值并返回布尔值表示验证结果
53
+ * @returns 返回一个接收错误消息的函数,该函数再返回实际的装饰器
54
+ *
55
+ * @example
56
+ * // 创建一个验证字符串的装饰器
57
+ * const VString = BaseValidator.decoratorCreator(
58
+ * (val) => typeof val === 'string' || val === undefined
59
+ * );
60
+ *
61
+ * // 创建一个验证必填项的装饰器
62
+ * const VRequired = BaseValidator.decoratorCreator(
63
+ * (val) => val !== undefined && val !== null && val !== ''
64
+ * );
65
+ *
66
+ * // 在类中使用这些装饰器
67
+ * class User extends BaseValidator {
68
+ * @VString('名称必须为字符串')
69
+ * @(VRequired()('名称必须填写'))
70
+ * name?: string;
71
+ *
72
+ * // 验证使用
73
+ * validateName() {
74
+ * return this.validate('name');
75
+ * }
76
+ * }
77
+ *
78
+ * const user = new User();
79
+ * console.log(user.validateName()); // 显示错误信息:名称必须填写
80
+ */
81
+ static decoratorCreator: (func: (val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => boolean) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
82
+ }
83
+
84
+ export { BaseValidator };