@anjianshi/utils 2.4.11 → 2.4.12
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 +3 -3
- package/validators/factory.d.ts +9 -2
- package/validators/factory.js +13 -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.12",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-packages/utils",
|
|
6
6
|
"bugs": {
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"typescript": "^5.6.3",
|
|
29
29
|
"vconsole": "^3.15.1",
|
|
30
30
|
"@anjianshi/presets-eslint-node": "4.0.14",
|
|
31
|
-
"@anjianshi/presets-eslint-
|
|
31
|
+
"@anjianshi/presets-eslint-typescript": "5.0.11",
|
|
32
32
|
"@anjianshi/presets-prettier": "3.0.1",
|
|
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/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,11 @@ 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)
|
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
|
+
}
|