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.
- package/README.md +144 -1
- package/{dist/cjs → cjs}/cache/index.cjs +28 -15
- package/{dist/types → cjs}/cache/index.d.ts +13 -7
- package/cjs/request/index.cjs +150 -0
- package/{dist/types → cjs}/request/index.d.ts +12 -8
- package/cjs/store/index.cjs +10 -0
- package/cjs/store/index.d.ts +2 -0
- package/cjs/validator/decorators.cjs +246 -0
- package/cjs/validator/decorators.d.ts +159 -0
- package/cjs/validator/index.cjs +19 -0
- package/cjs/validator/index.d.ts +2 -0
- package/{dist/cjs → cjs}/validator/validator.cjs +78 -13
- package/cjs/validator/validator.d.ts +84 -0
- package/es/cache/index.d.ts +141 -0
- package/{dist/es → es}/cache/index.mjs +28 -15
- package/es/cache/indexDB.d.ts +52 -0
- package/es/index.d.ts +7 -0
- package/es/request/error.d.ts +31 -0
- package/es/request/index.d.ts +140 -0
- package/es/request/index.mjs +148 -0
- package/es/store/createGetter/index.d.ts +30 -0
- package/es/store/createStateStore/index.d.ts +42 -0
- package/es/store/index.d.ts +2 -0
- package/es/store/index.mjs +2 -0
- package/es/validator/decorators.d.ts +159 -0
- package/es/validator/decorators.mjs +234 -0
- package/es/validator/index.d.ts +2 -0
- package/es/validator/index.mjs +2 -0
- package/es/validator/validator.d.ts +84 -0
- package/{dist/es → es}/validator/validator.mjs +78 -13
- package/package.json +12 -9
- package/dist/cjs/request/index.cjs +0 -129
- package/dist/cjs/validator/decorators.cjs +0 -104
- package/dist/es/request/index.mjs +0 -127
- package/dist/es/validator/decorators.mjs +0 -92
- package/dist/types/validator/decorators.d.ts +0 -15
- package/dist/types/validator/validator.d.ts +0 -24
- /package/{dist/cjs → cjs/_utils}/defaultEquals.cjs +0 -0
- /package/{dist/cjs → cjs}/cache/indexDB.cjs +0 -0
- /package/{dist/types → cjs}/cache/indexDB.d.ts +0 -0
- /package/{dist/cjs → cjs}/index.cjs +0 -0
- /package/{dist/types → cjs}/index.d.ts +0 -0
- /package/{dist/cjs → cjs}/request/defaultHandlers.cjs +0 -0
- /package/{dist/cjs → cjs}/request/error.cjs +0 -0
- /package/{dist/types → cjs}/request/error.d.ts +0 -0
- /package/{dist/cjs → cjs}/store/createGetter/index.cjs +0 -0
- /package/{dist/types → cjs}/store/createGetter/index.d.ts +0 -0
- /package/{dist/cjs → cjs}/store/createStateStore/index.cjs +0 -0
- /package/{dist/types → cjs}/store/createStateStore/index.d.ts +0 -0
- /package/{dist/es → es/_utils}/defaultEquals.mjs +0 -0
- /package/{dist/es → es}/cache/indexDB.mjs +0 -0
- /package/{dist/es → es}/index.mjs +0 -0
- /package/{dist/es → es}/request/defaultHandlers.mjs +0 -0
- /package/{dist/es → es}/request/error.mjs +0 -0
- /package/{dist/es → es}/store/createGetter/index.mjs +0 -0
- /package/{dist/es → es}/store/createStateStore/index.mjs +0 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 状态初始化设置器类型
|
|
3
|
+
* 用于延迟初始化状态的函数类型
|
|
4
|
+
*/
|
|
5
|
+
type IHookStateInitialSetter<S> = () => S;
|
|
6
|
+
/**
|
|
7
|
+
* 状态初始化动作类型
|
|
8
|
+
* 可以是直接的状态值或初始化设置器
|
|
9
|
+
*/
|
|
10
|
+
type IHookStateInitAction<S> = S | IHookStateInitialSetter<S>;
|
|
11
|
+
/**
|
|
12
|
+
* 状态设置器类型
|
|
13
|
+
* 可以是接收前一个状态的函数或无参数的函数
|
|
14
|
+
*/
|
|
15
|
+
type IHookStateSetter<S> = ((prevState: S) => S) | (() => S);
|
|
16
|
+
/**
|
|
17
|
+
* 状态设置动作类型
|
|
18
|
+
* 可以是直接的状态值或状态设置器
|
|
19
|
+
*/
|
|
20
|
+
type IHookStateSetAction<S> = S | IHookStateSetter<S>;
|
|
21
|
+
/**
|
|
22
|
+
* 可解析的状态类型
|
|
23
|
+
* 包含所有可能的状态值或状态设置函数
|
|
24
|
+
*/
|
|
25
|
+
type IHookStateResolvable<S> = S | IHookStateInitialSetter<S> | IHookStateSetter<S>;
|
|
26
|
+
/**
|
|
27
|
+
* 创建状态存储
|
|
28
|
+
* 提供一个简单的状态管理解决方案,支持组件间状态共享
|
|
29
|
+
*
|
|
30
|
+
* @template S 状态类型
|
|
31
|
+
* @param initialState 初始状态值或初始化函数
|
|
32
|
+
* @returns 包含状态操作方法的对象
|
|
33
|
+
*/
|
|
34
|
+
declare function createStateStore<S>(initialState?: S): {
|
|
35
|
+
use: () => [S, (state: IHookStateSetAction<S>) => void];
|
|
36
|
+
get: () => S;
|
|
37
|
+
set: (state: IHookStateSetAction<S>) => void;
|
|
38
|
+
watch: (callback: (state: S) => S | void) => () => void;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { createStateStore as default };
|
|
42
|
+
export type { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { GetterNameMap, ReducedData, StoreGetter, createStoreGetter, createStoreGetterMemo } from './createGetter/index.js';
|
|
2
|
+
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './createStateStore/index.js';
|
|
@@ -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,234 @@
|
|
|
1
|
+
import { BaseValidator } from './validator.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 验证器装饰器模块
|
|
5
|
+
* 提供一系列用于数据验证的装饰器,可用于类属性的验证规则定义
|
|
6
|
+
* 这些装饰器基于 BaseValidator 的 decoratorCreator 方法创建
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* 必填项验证装饰器
|
|
10
|
+
* 验证值是否存在且不在指定的无效值列表中
|
|
11
|
+
*
|
|
12
|
+
* @param noneVals 被视为无效的值数组,默认为 [undefined]
|
|
13
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* class User extends BaseValidator {
|
|
17
|
+
* @(VRequired()('用户名不能为空'))
|
|
18
|
+
* username?: string;
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
function VRequired(noneVals) {
|
|
22
|
+
if (noneVals === void 0) { noneVals = [undefined]; }
|
|
23
|
+
return BaseValidator.decoratorCreator(function (val) {
|
|
24
|
+
if (noneVals.includes(val)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 字符串类型验证装饰器
|
|
32
|
+
* 验证值是否为字符串类型
|
|
33
|
+
*
|
|
34
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* class User extends BaseValidator {
|
|
38
|
+
* @VString('用户名必须为字符串')
|
|
39
|
+
* username?: string;
|
|
40
|
+
* }
|
|
41
|
+
*/
|
|
42
|
+
var VString = BaseValidator.decoratorCreator(function (val) {
|
|
43
|
+
if (typeof val !== 'string') {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
});
|
|
48
|
+
/**
|
|
49
|
+
* 数字类型验证装饰器
|
|
50
|
+
* 验证值是否为数字类型
|
|
51
|
+
*
|
|
52
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* class User extends BaseValidator {
|
|
56
|
+
* @VNumber('年龄必须为数字')
|
|
57
|
+
* age?: number;
|
|
58
|
+
* }
|
|
59
|
+
*/
|
|
60
|
+
var VNumber = BaseValidator.decoratorCreator(function (val) {
|
|
61
|
+
if (typeof val !== 'number') {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* 数组类型验证装饰器
|
|
68
|
+
* 验证值是否为数组类型
|
|
69
|
+
*
|
|
70
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* class User extends BaseValidator {
|
|
74
|
+
* @VArray('标签必须为数组')
|
|
75
|
+
* tags?: string[];
|
|
76
|
+
* }
|
|
77
|
+
*/
|
|
78
|
+
var VArray = BaseValidator.decoratorCreator(function (val) {
|
|
79
|
+
if (!Array.isArray(val)) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* 布尔类型验证装饰器
|
|
86
|
+
* 验证值是否为布尔类型
|
|
87
|
+
*
|
|
88
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* class User extends BaseValidator {
|
|
92
|
+
* @VBoolean('状态必须为布尔值')
|
|
93
|
+
* active?: boolean;
|
|
94
|
+
* }
|
|
95
|
+
*/
|
|
96
|
+
var VBoolean = BaseValidator.decoratorCreator(function (val) {
|
|
97
|
+
if (typeof val !== 'boolean') {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
});
|
|
102
|
+
/**
|
|
103
|
+
* 最小值验证装饰器
|
|
104
|
+
* 验证数字是否大于或等于指定的最小值
|
|
105
|
+
*
|
|
106
|
+
* @param min 最小值
|
|
107
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* class User extends BaseValidator {
|
|
111
|
+
* @(VMin(18)('年龄必须大于或等于18岁'))
|
|
112
|
+
* age?: number;
|
|
113
|
+
* }
|
|
114
|
+
*/
|
|
115
|
+
var VMin = function (min) {
|
|
116
|
+
return BaseValidator.decoratorCreator(function (val) {
|
|
117
|
+
if (typeof val !== 'number' || val < min) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* 最大值验证装饰器
|
|
125
|
+
* 验证数字是否小于或等于指定的最大值
|
|
126
|
+
*
|
|
127
|
+
* @param max 最大值
|
|
128
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* class User extends BaseValidator {
|
|
132
|
+
* @(VMax(120)('年龄必须小于或等于120岁'))
|
|
133
|
+
* age?: number;
|
|
134
|
+
* }
|
|
135
|
+
*/
|
|
136
|
+
var VMax = function (max) {
|
|
137
|
+
return BaseValidator.decoratorCreator(function (val) {
|
|
138
|
+
if (typeof val !== 'number' || val > max) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* 最小长度验证装饰器
|
|
146
|
+
* 验证字符串或数组的长度是否大于或等于指定的最小长度
|
|
147
|
+
*
|
|
148
|
+
* @param minLen 最小长度
|
|
149
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* class User extends BaseValidator {
|
|
153
|
+
* @(VMinLength(6)('密码长度不能少于6位'))
|
|
154
|
+
* password?: string;
|
|
155
|
+
* }
|
|
156
|
+
*/
|
|
157
|
+
var VMinLength = function (minLen) {
|
|
158
|
+
return BaseValidator.decoratorCreator(function (val) {
|
|
159
|
+
if (typeof val !== 'string' && !Array.isArray(val)) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
if (val.length < minLen) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* 最大长度验证装饰器
|
|
170
|
+
* 验证字符串或数组的长度是否小于或等于指定的最大长度
|
|
171
|
+
*
|
|
172
|
+
* @param maxLen 最大长度
|
|
173
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* class User extends BaseValidator {
|
|
177
|
+
* @(VMaxLength(20)('用户名长度不能超过20位'))
|
|
178
|
+
* username?: string;
|
|
179
|
+
* }
|
|
180
|
+
*/
|
|
181
|
+
var VMaxLength = function (maxLen) {
|
|
182
|
+
return BaseValidator.decoratorCreator(function (val) {
|
|
183
|
+
if (typeof val !== 'string' && !Array.isArray(val)) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
if (val.length > maxLen) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
return true;
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* 邮箱格式验证装饰器
|
|
194
|
+
* 验证字符串是否符合邮箱格式
|
|
195
|
+
*
|
|
196
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* class User extends BaseValidator {
|
|
200
|
+
* @VEmail('邮箱格式不正确')
|
|
201
|
+
* email?: string;
|
|
202
|
+
* }
|
|
203
|
+
*/
|
|
204
|
+
var VEmail = BaseValidator.decoratorCreator(function (val) {
|
|
205
|
+
if (typeof val !== 'string') {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
// 简单邮箱正则
|
|
209
|
+
var emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
210
|
+
return emailReg.test(val);
|
|
211
|
+
});
|
|
212
|
+
/**
|
|
213
|
+
* 正则表达式验证装饰器
|
|
214
|
+
* 验证字符串是否匹配指定的正则表达式模式
|
|
215
|
+
*
|
|
216
|
+
* @param pattern 正则表达式
|
|
217
|
+
* @returns 装饰器工厂函数,可接收自定义错误消息
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* class User extends BaseValidator {
|
|
221
|
+
* @(VPattern(/^1[3-9]\d{9}$/)('手机号格式不正确'))
|
|
222
|
+
* phone?: string;
|
|
223
|
+
* }
|
|
224
|
+
*/
|
|
225
|
+
var VPattern = function (pattern) {
|
|
226
|
+
return BaseValidator.decoratorCreator(function (val) {
|
|
227
|
+
if (typeof val !== 'string') {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
return pattern.test(val);
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString };
|
|
@@ -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 };
|
|
@@ -1,15 +1,30 @@
|
|
|
1
1
|
import { __spreadArray } from 'tslib';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* 基础验证器类
|
|
5
|
+
* 提供字段验证功能,可通过装饰器为类属性添加验证规则
|
|
6
|
+
*/
|
|
3
7
|
var BaseValidator = /** @class */ (function () {
|
|
8
|
+
/**
|
|
9
|
+
* 构造函数
|
|
10
|
+
* 初始化验证器映射存储
|
|
11
|
+
*/
|
|
4
12
|
function BaseValidator() {
|
|
5
|
-
|
|
6
|
-
this.__keySymbol = Symbol(
|
|
13
|
+
/** 用于存储验证器映射的私有符号 */
|
|
14
|
+
this.__keySymbol = Symbol("key-description");
|
|
15
|
+
this.__keySymbol = Symbol("key-description");
|
|
7
16
|
this[this.__keySymbol] = {};
|
|
8
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* 验证单个字段
|
|
20
|
+
* @param itemKey 要验证的字段名
|
|
21
|
+
* @param itemAll 是否验证所有规则,为true时会验证该字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
22
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
23
|
+
*/
|
|
9
24
|
BaseValidator.prototype.validate = function (itemKey, itemAll) {
|
|
10
25
|
if (itemAll === void 0) { itemAll = false; }
|
|
11
26
|
var validatorMap = this[this.__keySymbol];
|
|
12
|
-
var errors =
|
|
27
|
+
var errors = {};
|
|
13
28
|
// 校验每个 key
|
|
14
29
|
var validators = validatorMap[itemKey];
|
|
15
30
|
if (!validators) {
|
|
@@ -19,21 +34,33 @@ var BaseValidator = /** @class */ (function () {
|
|
|
19
34
|
var validator = validators_1[_i];
|
|
20
35
|
var res = validator(this[itemKey]);
|
|
21
36
|
if (!res.status) {
|
|
22
|
-
|
|
37
|
+
if (Array.isArray(errors[res.name])) {
|
|
38
|
+
errors[res.name].push(res.message);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
errors[res.name] = [res.message];
|
|
42
|
+
}
|
|
23
43
|
if (!itemAll)
|
|
24
44
|
break;
|
|
25
45
|
}
|
|
26
46
|
}
|
|
27
|
-
if (errors.length) {
|
|
47
|
+
if (Object.keys(errors).length) {
|
|
28
48
|
return errors;
|
|
29
49
|
}
|
|
30
50
|
return null;
|
|
31
51
|
};
|
|
32
|
-
|
|
52
|
+
/**
|
|
53
|
+
* 验证多个或所有字段
|
|
54
|
+
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止
|
|
55
|
+
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止
|
|
56
|
+
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序
|
|
57
|
+
* @returns 验证错误数组,如果没有错误则返回null
|
|
58
|
+
*/
|
|
59
|
+
BaseValidator.prototype.validateAll = function (order, itemAll, everyItem) {
|
|
33
60
|
if (itemAll === void 0) { itemAll = false; }
|
|
34
61
|
if (everyItem === void 0) { everyItem = false; }
|
|
35
62
|
var validatorMap = this[this.__keySymbol];
|
|
36
|
-
var errors =
|
|
63
|
+
var errors = {};
|
|
37
64
|
// 校验每个 key
|
|
38
65
|
var keys = order || Object.keys(validatorMap);
|
|
39
66
|
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
@@ -45,7 +72,12 @@ var BaseValidator = /** @class */ (function () {
|
|
|
45
72
|
var fn = fns_1[_a];
|
|
46
73
|
var res = fn(value);
|
|
47
74
|
if (!res.status) {
|
|
48
|
-
|
|
75
|
+
if (Array.isArray(errors[res.name])) {
|
|
76
|
+
errors[res.name].push(res.message);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
errors[res.name] = [res.message];
|
|
80
|
+
}
|
|
49
81
|
if (!itemAll)
|
|
50
82
|
break;
|
|
51
83
|
}
|
|
@@ -54,11 +86,44 @@ var BaseValidator = /** @class */ (function () {
|
|
|
54
86
|
break;
|
|
55
87
|
}
|
|
56
88
|
}
|
|
57
|
-
if (errors.length) {
|
|
89
|
+
if (Object.keys(errors).length) {
|
|
58
90
|
return errors;
|
|
59
91
|
}
|
|
60
92
|
return null;
|
|
61
93
|
};
|
|
94
|
+
/**
|
|
95
|
+
* 装饰器创建器
|
|
96
|
+
* 用于创建属性验证装饰器的工厂函数
|
|
97
|
+
*
|
|
98
|
+
* @param func 验证函数,接收属性值并返回布尔值表示验证结果
|
|
99
|
+
* @returns 返回一个接收错误消息的函数,该函数再返回实际的装饰器
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* // 创建一个验证字符串的装饰器
|
|
103
|
+
* const VString = BaseValidator.decoratorCreator(
|
|
104
|
+
* (val) => typeof val === 'string' || val === undefined
|
|
105
|
+
* );
|
|
106
|
+
*
|
|
107
|
+
* // 创建一个验证必填项的装饰器
|
|
108
|
+
* const VRequired = BaseValidator.decoratorCreator(
|
|
109
|
+
* (val) => val !== undefined && val !== null && val !== ''
|
|
110
|
+
* );
|
|
111
|
+
*
|
|
112
|
+
* // 在类中使用这些装饰器
|
|
113
|
+
* class User extends BaseValidator {
|
|
114
|
+
* @VString('名称必须为字符串')
|
|
115
|
+
* @(VRequired()('名称必须填写'))
|
|
116
|
+
* name?: string;
|
|
117
|
+
*
|
|
118
|
+
* // 验证使用
|
|
119
|
+
* validateName() {
|
|
120
|
+
* return this.validate('name');
|
|
121
|
+
* }
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* const user = new User();
|
|
125
|
+
* console.log(user.validateName()); // 显示错误信息:名称必须填写
|
|
126
|
+
*/
|
|
62
127
|
BaseValidator.decoratorCreator = function (func) {
|
|
63
128
|
return function (message) {
|
|
64
129
|
if (message === void 0) { message = function (val, value, context) { return "".concat(String(context.name), "\u683C\u5F0F\u9519\u8BEF"); }; }
|
|
@@ -73,17 +138,17 @@ var BaseValidator = /** @class */ (function () {
|
|
|
73
138
|
var validator = function (val) {
|
|
74
139
|
var validateStatus = func(val, value, context);
|
|
75
140
|
if (validateStatus) {
|
|
76
|
-
return { status: true };
|
|
141
|
+
return { name: name, status: true };
|
|
77
142
|
}
|
|
78
143
|
else {
|
|
79
|
-
var msg =
|
|
80
|
-
if (typeof message ===
|
|
144
|
+
var msg = "";
|
|
145
|
+
if (typeof message === "function") {
|
|
81
146
|
msg = message(val, value, context);
|
|
82
147
|
}
|
|
83
148
|
else {
|
|
84
149
|
msg = message;
|
|
85
150
|
}
|
|
86
|
-
return { status: false, message: msg };
|
|
151
|
+
return { name: name, status: false, message: msg };
|
|
87
152
|
}
|
|
88
153
|
};
|
|
89
154
|
if (validators[name]) {
|