@anjianshi/utils 2.4.11 → 2.4.13
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/package.json +4 -4
- package/validators/base.d.ts +3 -3
- package/validators/base.js +1 -0
- package/validators/factory.d.ts +9 -2
- package/validators/factory.js +20 -0
- package/validators/index.d.ts +1 -0
- package/validators/index.js +1 -0
- package/validators/oneOf.d.ts +10 -0
- package/validators/oneOf.js +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anjianshi/utils",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.13",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-packages/utils",
|
|
6
6
|
"bugs": {
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"redis": "^4.7.0",
|
|
28
28
|
"typescript": "^5.6.3",
|
|
29
29
|
"vconsole": "^3.15.1",
|
|
30
|
-
"@anjianshi/presets-eslint-
|
|
31
|
-
"@anjianshi/presets-eslint-react": "4.0.13",
|
|
30
|
+
"@anjianshi/presets-eslint-typescript": "5.0.11",
|
|
32
31
|
"@anjianshi/presets-prettier": "3.0.1",
|
|
32
|
+
"@anjianshi/presets-eslint-node": "4.0.14",
|
|
33
33
|
"@anjianshi/presets-typescript": "3.2.3",
|
|
34
|
-
"@anjianshi/presets-eslint-
|
|
34
|
+
"@anjianshi/presets-eslint-react": "4.0.13"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"@emotion/react": "^11.13.3",
|
package/validators/base.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type PrimitiveType = string | boolean | number | PrimitiveType[] | [...Pr
|
|
|
12
12
|
/**
|
|
13
13
|
* validator 通用参数
|
|
14
14
|
*/
|
|
15
|
-
export interface CommonOptions<Value =
|
|
15
|
+
export interface CommonOptions<Value = any> {
|
|
16
16
|
/** 是否允许 null 值 @default false */
|
|
17
17
|
null?: boolean;
|
|
18
18
|
/** 字段是否必须有值(不能是 undefined) @default true */
|
|
@@ -22,7 +22,7 @@ export interface CommonOptions<Value = unknown> {
|
|
|
22
22
|
* 指定后 required 选项将失去作用。
|
|
23
23
|
*/
|
|
24
24
|
defaults?: Value;
|
|
25
|
-
custom?:
|
|
25
|
+
custom?: (input: Value) => MaySuccess<Value>;
|
|
26
26
|
[key: string]: unknown;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
@@ -78,5 +78,5 @@ export declare function getValidatorGenerator<Value, Options extends CommonOptio
|
|
|
78
78
|
* 返回只进行基本检查,不带定制的验证逻辑的 validator。
|
|
79
79
|
* 同时也是定制 validator 最小化实现的例子。
|
|
80
80
|
*/
|
|
81
|
-
export declare const getAnyValidator: <const InputOptions extends CommonOptions<
|
|
81
|
+
export declare const getAnyValidator: <const InputOptions extends CommonOptions<any>>(inputOptions: InputOptions) => Validator<unknown, InputOptions>;
|
|
82
82
|
export {};
|
package/validators/base.js
CHANGED
|
@@ -7,6 +7,7 @@ import { success, failed } from '../lang/index.js';
|
|
|
7
7
|
export function getValidatorGenerator(validate) {
|
|
8
8
|
return function validatorGenerator(inputOptions) {
|
|
9
9
|
function validator(field, input) {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
10
11
|
const { null: allowNull = false, required = true, defaults, custom } = inputOptions;
|
|
11
12
|
if (typeof field !== 'string') {
|
|
12
13
|
input = field;
|
package/validators/factory.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { type Validator, type CommonOptions, type Validated } from './base.js';
|
|
|
6
6
|
import { type BooleanOptions } from './boolean.js';
|
|
7
7
|
import { type NumberOptions, type NumberValueWithChoices } from './number.js';
|
|
8
8
|
import { type RecordOptions, type StructOptions } from './object.js';
|
|
9
|
+
import { type OneOfOptions } from './oneOf.js';
|
|
9
10
|
import { type StringOptions, type StringValueWithChoices } from './string.js';
|
|
10
11
|
export interface AnyDefinition extends CommonOptions {
|
|
11
12
|
type: 'any';
|
|
@@ -35,12 +36,18 @@ export interface RecordDefinition extends Omit<RecordOptions, 'record'> {
|
|
|
35
36
|
type: 'record';
|
|
36
37
|
record: Definition;
|
|
37
38
|
}
|
|
38
|
-
export
|
|
39
|
+
export interface OneOfDefinition extends Omit<OneOfOptions, 'validators'> {
|
|
40
|
+
type: 'oneOf';
|
|
41
|
+
validators: Definition[];
|
|
42
|
+
}
|
|
43
|
+
export type Definition = AnyDefinition | BooleanDefinition | NumberDefinition | StringDefinition | ArrayDefinition | TupleDefinition | StructDefinition | RecordDefinition | OneOfDefinition;
|
|
39
44
|
export type ValueOfDefinition<Def extends Definition> = Def extends AnyDefinition ? unknown : Def extends BooleanDefinition ? boolean : Def extends NumberDefinition ? NumberValueWithChoices<Def> : Def extends StringDefinition ? StringValueWithChoices<Def> : Def extends ArrayDefinition ? Validated<ValueOfDefinition<Def['item']>, Def['item']>[] : Def extends TupleDefinition ? {
|
|
40
45
|
[Key in keyof Def['tuple']]: Def['tuple'][Key] extends Definition ? Validated<ValueOfDefinition<Def['tuple'][Key]>, Def['tuple'][Key]> : Def['tuple'][Key];
|
|
41
46
|
} : Def extends StructDefinition ? {
|
|
42
47
|
[Key in keyof Def['struct']]: Validated<ValueOfDefinition<Def['struct'][Key]>, Def['struct'][Key]>;
|
|
43
|
-
} : Def extends RecordDefinition ? Record<string, Validated<ValueOfDefinition<Def['record']>, Def['record']>> :
|
|
48
|
+
} : Def extends RecordDefinition ? Record<string, Validated<ValueOfDefinition<Def['record']>, Def['record']>> : Def extends OneOfDefinition ? {
|
|
49
|
+
[Key in keyof Def['validators']]: Def['validators'][Key] extends Definition ? Validated<ValueOfDefinition<Def['validators'][Key]>, Def['validators'][Key]> : Def['validators'][Key];
|
|
50
|
+
}[number] : never;
|
|
44
51
|
export type OptionsFromDefinition<Def extends Definition> = Def extends ArrayDefinition ? Omit<Def, 'item'> & {
|
|
45
52
|
item: ValidatorForDefinition<Def['item']>;
|
|
46
53
|
} : Def extends TupleDefinition ? Omit<Def, 'tuple'> & {
|
package/validators/factory.js
CHANGED
|
@@ -6,6 +6,7 @@ import { getAnyValidator } from './base.js';
|
|
|
6
6
|
import { getBooleanValidator } from './boolean.js';
|
|
7
7
|
import { getNumberValidator } from './number.js';
|
|
8
8
|
import { getRecordValidator, getStructValidator, } from './object.js';
|
|
9
|
+
import { getOneOfValidator } from './oneOf.js';
|
|
9
10
|
import { getStringValidator } from './string.js';
|
|
10
11
|
export function getValidator(definition) {
|
|
11
12
|
switch (definition.type) {
|
|
@@ -40,6 +41,10 @@ export function getValidator(definition) {
|
|
|
40
41
|
...definition,
|
|
41
42
|
record: getValidator(definition.record),
|
|
42
43
|
});
|
|
44
|
+
case 'oneOf':
|
|
45
|
+
return getOneOfValidator({
|
|
46
|
+
validators: definition.validators.map(def => getValidator(def)),
|
|
47
|
+
});
|
|
43
48
|
}
|
|
44
49
|
}
|
|
45
50
|
// ---------------- 测试用例 -----------------
|
|
@@ -86,3 +91,18 @@ export function getValidator(definition) {
|
|
|
86
91
|
// type: 'record',
|
|
87
92
|
// record: { type: 'string', null: true, choices: ['a', 'b', 'c'] },
|
|
88
93
|
// })(1)
|
|
94
|
+
// const v10 = getValidator({
|
|
95
|
+
// type: 'oneOf',
|
|
96
|
+
// validators: [
|
|
97
|
+
// { type: 'string', choices: ['a', 'b', 'c'] },
|
|
98
|
+
// { type: 'number', null: true },
|
|
99
|
+
// ],
|
|
100
|
+
// defaults: true,
|
|
101
|
+
// })(1)
|
|
102
|
+
// const v11 = getValidator({
|
|
103
|
+
// type: 'string',
|
|
104
|
+
// custom(value) {
|
|
105
|
+
// return { success: true, data: value }
|
|
106
|
+
// },
|
|
107
|
+
// defaults: 'some text',
|
|
108
|
+
// })(1)
|
package/validators/index.d.ts
CHANGED
package/validators/index.js
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type CommonOptions, type Validator } from './base.js';
|
|
2
|
+
/** 要求返回值通过其中一个验证器的检查 */
|
|
3
|
+
export interface OneOfOptions extends CommonOptions {
|
|
4
|
+
/** 验证数组各元素 */
|
|
5
|
+
validators: Validator<unknown, CommonOptions>[];
|
|
6
|
+
}
|
|
7
|
+
export type OneOfValue<Options extends OneOfOptions> = Options extends {
|
|
8
|
+
validators: Validator<infer T, CommonOptions>[];
|
|
9
|
+
} ? T : never;
|
|
10
|
+
export declare function getOneOfValidator<const Options extends OneOfOptions>(options?: Options): Validator<OneOfValue<Options>, Options>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { failed } from '../lang/index.js';
|
|
2
|
+
import { getValidatorGenerator, } from './base.js';
|
|
3
|
+
export function getOneOfValidator(options = {}) {
|
|
4
|
+
return getValidatorGenerator(function validate(field, value) {
|
|
5
|
+
for (const validator of options.validators) {
|
|
6
|
+
const result = validator(field, value);
|
|
7
|
+
if (result.success)
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
return failed(`${field} do not match any valid format`);
|
|
11
|
+
})(options);
|
|
12
|
+
}
|