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,93 +1,55 @@
|
|
|
1
|
-
import { useState, useLayoutEffect, useEffect } from
|
|
2
|
-
|
|
1
|
+
import { useState, useLayoutEffect, useEffect } from "react";
|
|
3
2
|
function resolveHookState(nextState, currentState) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
return nextState;
|
|
3
|
+
if (typeof nextState === "function") {
|
|
4
|
+
return nextState.length ? nextState(currentState) : nextState();
|
|
5
|
+
}
|
|
6
|
+
return nextState;
|
|
10
7
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* 只执行一次的 useEffect hook
|
|
15
|
-
* @param effect 要执行的副作用函数
|
|
16
|
-
*/
|
|
17
|
-
var useEffectOnce = function (effect) {
|
|
18
|
-
useEffect(effect, []);
|
|
8
|
+
const isBrowser = typeof window !== "undefined";
|
|
9
|
+
const useEffectOnce = (effect) => {
|
|
10
|
+
useEffect(effect, []);
|
|
19
11
|
};
|
|
20
|
-
|
|
21
|
-
* 同构的 useLayoutEffect
|
|
22
|
-
* 在服务端渲染时使用 useEffect,在浏览器环境使用 useLayoutEffect
|
|
23
|
-
*/
|
|
24
|
-
var useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
|
|
25
|
-
/**
|
|
26
|
-
* 创建状态存储
|
|
27
|
-
* 提供一个简单的状态管理解决方案,支持组件间状态共享
|
|
28
|
-
*
|
|
29
|
-
* @template S 状态类型
|
|
30
|
-
* @param initialState 初始状态值或初始化函数
|
|
31
|
-
* @returns 包含状态操作方法的对象
|
|
32
|
-
*/
|
|
12
|
+
const useIsomorphicLayoutEffect = isBrowser ? useLayoutEffect : useEffect;
|
|
33
13
|
function createStateStore(initialState) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
useIsomorphicLayoutEffect(function () {
|
|
63
|
-
if (!store.setters.includes(stateSetter)) {
|
|
64
|
-
store.setters.push(stateSetter);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
return [globalState, store.setState];
|
|
68
|
-
};
|
|
69
|
-
/** 获取当前状态值 */
|
|
70
|
-
var get = function () { return store.state; };
|
|
71
|
-
/** 设置状态的函数引用 */
|
|
72
|
-
var set = store.setState;
|
|
73
|
-
/**
|
|
74
|
-
* 监听状态变化
|
|
75
|
-
* @param callback 状态变化时的回调函数
|
|
76
|
-
* @returns 取消监听的函数
|
|
77
|
-
*/
|
|
78
|
-
var watch = function (callback) {
|
|
79
|
-
store.watchers.push(callback);
|
|
80
|
-
var close = function () {
|
|
81
|
-
store.watchers = store.watchers.filter(function (watcher) { return watcher !== callback; });
|
|
82
|
-
};
|
|
83
|
-
return close;
|
|
84
|
-
};
|
|
85
|
-
return {
|
|
86
|
-
use: use,
|
|
87
|
-
get: get,
|
|
88
|
-
set: set,
|
|
89
|
-
watch: watch,
|
|
14
|
+
const store = {
|
|
15
|
+
state: initialState instanceof Function ? initialState() : initialState,
|
|
16
|
+
setState(nextState) {
|
|
17
|
+
store.state = resolveHookState(nextState, store.state);
|
|
18
|
+
store.setters.forEach((setter) => setter(store.state));
|
|
19
|
+
store.watchers.forEach((watcher) => watcher(store.state));
|
|
20
|
+
},
|
|
21
|
+
setters: [],
|
|
22
|
+
watchers: []
|
|
23
|
+
};
|
|
24
|
+
const use = () => {
|
|
25
|
+
const [globalState, stateSetter] = useState(store.state);
|
|
26
|
+
useEffectOnce(() => () => {
|
|
27
|
+
store.setters = store.setters.filter((setter) => setter !== stateSetter);
|
|
28
|
+
});
|
|
29
|
+
useIsomorphicLayoutEffect(() => {
|
|
30
|
+
if (!store.setters.includes(stateSetter)) {
|
|
31
|
+
store.setters.push(stateSetter);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return [globalState, store.setState];
|
|
35
|
+
};
|
|
36
|
+
const get = () => store.state;
|
|
37
|
+
const set = store.setState;
|
|
38
|
+
const watch = (callback) => {
|
|
39
|
+
store.watchers.push(callback);
|
|
40
|
+
const close = () => {
|
|
41
|
+
store.watchers = store.watchers.filter((watcher) => watcher !== callback);
|
|
90
42
|
};
|
|
43
|
+
return close;
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
use,
|
|
47
|
+
get,
|
|
48
|
+
set,
|
|
49
|
+
watch
|
|
50
|
+
};
|
|
91
51
|
}
|
|
92
|
-
|
|
93
|
-
|
|
52
|
+
export {
|
|
53
|
+
createStateStore as default,
|
|
54
|
+
resolveHookState
|
|
55
|
+
};
|
package/es/store/index.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { createStoreGetter, createStoreGetterMemo } from './createGetter';
|
|
2
|
+
export { default as createStateStore } from './createStateStore';
|
|
3
|
+
export type { StoreGetter, GetterNameMap, ReducedData } from './createGetter';
|
|
4
|
+
export type { IHookStateInitialSetter, IHookStateInitAction, IHookStateSetter, IHookStateSetAction, IHookStateResolvable, } from './createStateStore';
|
package/es/store/index.mjs
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { createStoreGetter, createStoreGetterMemo } from "./createGetter/index.mjs";
|
|
2
|
+
import { default as default2 } from "./createStateStore/index.mjs";
|
|
3
|
+
export {
|
|
4
|
+
default2 as createStateStore,
|
|
5
|
+
createStoreGetter,
|
|
6
|
+
createStoreGetterMemo
|
|
7
|
+
};
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import { BaseValidator } from './validator
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 验证器装饰器模块
|
|
5
|
-
* 提供一系列用于数据验证的装饰器,可用于类属性的验证规则定义
|
|
6
|
-
* 这些装饰器基于 BaseValidator 的 decoratorCreator 方法创建
|
|
7
|
-
*/
|
|
8
|
-
|
|
1
|
+
import { BaseValidator } from './validator';
|
|
9
2
|
/**
|
|
10
3
|
* 必填项验证装饰器
|
|
11
4
|
* 验证值是否存在且不在指定的无效值列表中
|
|
@@ -19,7 +12,7 @@ import { BaseValidator } from './validator.js';
|
|
|
19
12
|
* username?: string;
|
|
20
13
|
* }
|
|
21
14
|
*/
|
|
22
|
-
declare function VRequired(noneVals?: any[]): (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
15
|
+
export declare function VRequired(noneVals?: any[]): (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
23
16
|
/**
|
|
24
17
|
* 字符串类型验证装饰器
|
|
25
18
|
* 验证值是否为字符串类型
|
|
@@ -32,7 +25,7 @@ declare function VRequired(noneVals?: any[]): (message?: ((val: any, value: unde
|
|
|
32
25
|
* username?: string;
|
|
33
26
|
* }
|
|
34
27
|
*/
|
|
35
|
-
declare const VString: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
28
|
+
export declare const VString: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
36
29
|
/**
|
|
37
30
|
* 数字类型验证装饰器
|
|
38
31
|
* 验证值是否为数字类型
|
|
@@ -45,7 +38,7 @@ declare const VString: (message?: ((val: any, value: undefined, context: ClassFi
|
|
|
45
38
|
* age?: number;
|
|
46
39
|
* }
|
|
47
40
|
*/
|
|
48
|
-
declare const VNumber: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
41
|
+
export declare const VNumber: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
49
42
|
/**
|
|
50
43
|
* 数组类型验证装饰器
|
|
51
44
|
* 验证值是否为数组类型
|
|
@@ -58,7 +51,7 @@ declare const VNumber: (message?: ((val: any, value: undefined, context: ClassFi
|
|
|
58
51
|
* tags?: string[];
|
|
59
52
|
* }
|
|
60
53
|
*/
|
|
61
|
-
declare const VArray: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
54
|
+
export declare const VArray: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
62
55
|
/**
|
|
63
56
|
* 布尔类型验证装饰器
|
|
64
57
|
* 验证值是否为布尔类型
|
|
@@ -71,7 +64,7 @@ declare const VArray: (message?: ((val: any, value: undefined, context: ClassFie
|
|
|
71
64
|
* active?: boolean;
|
|
72
65
|
* }
|
|
73
66
|
*/
|
|
74
|
-
declare const VBoolean: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
67
|
+
export declare const VBoolean: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
75
68
|
/**
|
|
76
69
|
* 最小值验证装饰器
|
|
77
70
|
* 验证数字是否大于或等于指定的最小值
|
|
@@ -85,7 +78,7 @@ declare const VBoolean: (message?: ((val: any, value: undefined, context: ClassF
|
|
|
85
78
|
* age?: number;
|
|
86
79
|
* }
|
|
87
80
|
*/
|
|
88
|
-
declare const VMin: (min: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
81
|
+
export declare const VMin: (min: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
89
82
|
/**
|
|
90
83
|
* 最大值验证装饰器
|
|
91
84
|
* 验证数字是否小于或等于指定的最大值
|
|
@@ -99,7 +92,7 @@ declare const VMin: (min: number) => (message?: ((val: any, value: undefined, co
|
|
|
99
92
|
* age?: number;
|
|
100
93
|
* }
|
|
101
94
|
*/
|
|
102
|
-
declare const VMax: (max: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
95
|
+
export declare const VMax: (max: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
103
96
|
/**
|
|
104
97
|
* 最小长度验证装饰器
|
|
105
98
|
* 验证字符串或数组的长度是否大于或等于指定的最小长度
|
|
@@ -113,7 +106,7 @@ declare const VMax: (max: number) => (message?: ((val: any, value: undefined, co
|
|
|
113
106
|
* password?: string;
|
|
114
107
|
* }
|
|
115
108
|
*/
|
|
116
|
-
declare const VMinLength: (minLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
109
|
+
export declare const VMinLength: (minLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
117
110
|
/**
|
|
118
111
|
* 最大长度验证装饰器
|
|
119
112
|
* 验证字符串或数组的长度是否小于或等于指定的最大长度
|
|
@@ -127,7 +120,7 @@ declare const VMinLength: (minLen: number) => (message?: ((val: any, value: unde
|
|
|
127
120
|
* username?: string;
|
|
128
121
|
* }
|
|
129
122
|
*/
|
|
130
|
-
declare const VMaxLength: (maxLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
123
|
+
export declare const VMaxLength: (maxLen: number) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
131
124
|
/**
|
|
132
125
|
* 邮箱格式验证装饰器
|
|
133
126
|
* 验证字符串是否符合邮箱格式
|
|
@@ -140,7 +133,7 @@ declare const VMaxLength: (maxLen: number) => (message?: ((val: any, value: unde
|
|
|
140
133
|
* email?: string;
|
|
141
134
|
* }
|
|
142
135
|
*/
|
|
143
|
-
declare const VEmail: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
136
|
+
export declare const VEmail: (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
144
137
|
/**
|
|
145
138
|
* 正则表达式验证装饰器
|
|
146
139
|
* 验证字符串是否匹配指定的正则表达式模式
|
|
@@ -154,6 +147,4 @@ declare const VEmail: (message?: ((val: any, value: undefined, context: ClassFie
|
|
|
154
147
|
* phone?: string;
|
|
155
148
|
* }
|
|
156
149
|
*/
|
|
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 };
|
|
150
|
+
export declare const VPattern: (pattern: RegExp) => (message?: ((val: any, value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => string) | string) => (value: undefined, context: ClassFieldDecoratorContext<BaseValidator>) => void;
|
|
@@ -1,234 +1,89 @@
|
|
|
1
|
-
import { BaseValidator } from
|
|
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;
|
|
1
|
+
import { BaseValidator } from "./validator.mjs";
|
|
2
|
+
function VRequired(noneVals = [void 0]) {
|
|
3
|
+
return BaseValidator.decoratorCreator((val) => {
|
|
4
|
+
if (noneVals.includes(val)) {
|
|
5
|
+
return false;
|
|
45
6
|
}
|
|
46
7
|
return true;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
const VString = BaseValidator.decoratorCreator((val) => {
|
|
11
|
+
if (typeof val !== "string") {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
47
15
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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;
|
|
16
|
+
const VNumber = BaseValidator.decoratorCreator((val) => {
|
|
17
|
+
if (typeof val !== "number") {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return true;
|
|
65
21
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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;
|
|
22
|
+
const VArray = BaseValidator.decoratorCreator((val) => {
|
|
23
|
+
if (!Array.isArray(val)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
83
27
|
});
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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;
|
|
28
|
+
const VBoolean = BaseValidator.decoratorCreator((val) => {
|
|
29
|
+
if (typeof val !== "boolean") {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
101
33
|
});
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
* @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);
|
|
34
|
+
const VMin = (min) => BaseValidator.decoratorCreator((val) => {
|
|
35
|
+
if (typeof val !== "number" || val < min) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
});
|
|
40
|
+
const VMax = (max) => BaseValidator.decoratorCreator((val) => {
|
|
41
|
+
if (typeof val !== "number" || val > max) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
});
|
|
46
|
+
const VMinLength = (minLen) => BaseValidator.decoratorCreator((val) => {
|
|
47
|
+
if (typeof val !== "string" && !Array.isArray(val)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (val.length < minLen) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
});
|
|
55
|
+
const VMaxLength = (maxLen) => BaseValidator.decoratorCreator((val) => {
|
|
56
|
+
if (typeof val !== "string" && !Array.isArray(val)) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (val.length > maxLen) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
});
|
|
64
|
+
const VEmail = BaseValidator.decoratorCreator((val) => {
|
|
65
|
+
if (typeof val !== "string") {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
const emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
69
|
+
return emailReg.test(val);
|
|
70
|
+
});
|
|
71
|
+
const VPattern = (pattern) => BaseValidator.decoratorCreator((val) => {
|
|
72
|
+
if (typeof val !== "string") {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
return pattern.test(val);
|
|
211
76
|
});
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
});
|
|
77
|
+
export {
|
|
78
|
+
VArray,
|
|
79
|
+
VBoolean,
|
|
80
|
+
VEmail,
|
|
81
|
+
VMax,
|
|
82
|
+
VMaxLength,
|
|
83
|
+
VMin,
|
|
84
|
+
VMinLength,
|
|
85
|
+
VNumber,
|
|
86
|
+
VPattern,
|
|
87
|
+
VRequired,
|
|
88
|
+
VString
|
|
232
89
|
};
|
|
233
|
-
|
|
234
|
-
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString };
|
package/es/validator/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { BaseValidator } from './validator
|
|
2
|
-
export {
|
|
1
|
+
export { BaseValidator } from './validator';
|
|
2
|
+
export { VRequired, VString, VNumber, VEmail, VMinLength, VArray, VBoolean, VPattern, VMaxLength, VMax, VMin, } from './decorators';
|
package/es/validator/index.mjs
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { BaseValidator } from "./validator.mjs";
|
|
2
|
+
import { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from "./decorators.mjs";
|
|
3
|
+
export {
|
|
4
|
+
BaseValidator,
|
|
5
|
+
VArray,
|
|
6
|
+
VBoolean,
|
|
7
|
+
VEmail,
|
|
8
|
+
VMax,
|
|
9
|
+
VMaxLength,
|
|
10
|
+
VMin,
|
|
11
|
+
VMinLength,
|
|
12
|
+
VNumber,
|
|
13
|
+
VPattern,
|
|
14
|
+
VRequired,
|
|
15
|
+
VString
|
|
16
|
+
};
|
|
@@ -18,7 +18,7 @@ type ValidatorMap = {
|
|
|
18
18
|
* 基础验证器类
|
|
19
19
|
* 提供字段验证功能,可通过装饰器为类属性添加验证规则
|
|
20
20
|
*/
|
|
21
|
-
declare class BaseValidator {
|
|
21
|
+
export declare class BaseValidator {
|
|
22
22
|
/** 用于存储验证器映射的私有符号 */
|
|
23
23
|
private __keySymbol;
|
|
24
24
|
/** 用于存储验证器映射的索引签名 */
|
|
@@ -39,9 +39,9 @@ declare class BaseValidator {
|
|
|
39
39
|
validate(itemKey: string, itemAll?: boolean): string[] | null;
|
|
40
40
|
/**
|
|
41
41
|
* 验证多个或所有字段
|
|
42
|
-
* @param order
|
|
43
|
-
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false
|
|
44
|
-
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false
|
|
42
|
+
* @param order 验证字段的顺序,可以指定验证的字段名数组及其顺序,默认验证所有字段,按对象定义顺序
|
|
43
|
+
* @param itemAll 是否验证每个字段的所有规则,为true时会验证字段的所有规则,为false时遇到第一个失败的规则就停止,默认为 false
|
|
44
|
+
* @param everyItem 是否验证所有字段,为true时会验证所有字段,为false时遇到第一个失败的字段就停止,默认为 false
|
|
45
45
|
* @returns 验证错误数组,如果没有错误则返回null
|
|
46
46
|
*/
|
|
47
47
|
validateAll(order?: string[], itemAll?: boolean, everyItem?: boolean): Record<string, string[]> | null;
|
|
@@ -80,5 +80,4 @@ declare class BaseValidator {
|
|
|
80
80
|
*/
|
|
81
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
82
|
}
|
|
83
|
-
|
|
84
|
-
export { BaseValidator };
|
|
83
|
+
export {};
|