@anjianshi/utils 2.4.10 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "2.4.10",
3
+ "version": "2.4.12",
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-node": "4.0.13",
31
- "@anjianshi/presets-eslint-react": "4.0.12",
32
- "@anjianshi/presets-eslint-typescript": "5.0.10",
30
+ "@anjianshi/presets-eslint-node": "4.0.14",
31
+ "@anjianshi/presets-eslint-typescript": "5.0.11",
33
32
  "@anjianshi/presets-prettier": "3.0.1",
34
- "@anjianshi/presets-typescript": "3.2.3"
33
+ "@anjianshi/presets-typescript": "3.2.3",
34
+ "@anjianshi/presets-eslint-react": "4.0.13"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "@emotion/react": "^11.13.3",
@@ -12,7 +12,7 @@ export type PrimitiveType = string | boolean | number | PrimitiveType[] | [...Pr
12
12
  /**
13
13
  * validator 通用参数
14
14
  */
15
- export interface CommonOptions<Defaults = unknown> {
15
+ export interface CommonOptions<Value = unknown> {
16
16
  /** 是否允许 null 值 @default false */
17
17
  null?: boolean;
18
18
  /** 字段是否必须有值(不能是 undefined) @default true */
@@ -21,7 +21,8 @@ export interface CommonOptions<Defaults = unknown> {
21
21
  * 默认值,字段无值(或值为 undefined)时生效,值为 null 不会生效。
22
22
  * 指定后 required 选项将失去作用。
23
23
  */
24
- defaults?: Defaults;
24
+ defaults?: Value;
25
+ custom?: <T extends Value>(input: T) => MaySuccess<T>;
25
26
  [key: string]: unknown;
26
27
  }
27
28
  /**
@@ -33,6 +34,9 @@ type FullfiledOptions<Options extends Partial<CommonOptions>> = Omit<Options, ke
33
34
  defaults: Options extends {
34
35
  defaults: infer T;
35
36
  } ? T : undefined;
37
+ custom: Options extends {
38
+ custom: infer T;
39
+ } ? T : undefined;
36
40
  };
37
41
  /**
38
42
  * 验证完成后能得到的值类型
@@ -7,7 +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
- const { null: allowNull = false, required = true, defaults } = inputOptions;
10
+ const { null: allowNull = false, required = true, defaults, custom } = inputOptions;
11
11
  if (typeof field !== 'string') {
12
12
  input = field;
13
13
  field = 'value';
@@ -25,7 +25,8 @@ export function getValidatorGenerator(validate) {
25
25
  return failed(`${field} cannot be null`);
26
26
  if (value === null || value === undefined)
27
27
  return success(value);
28
- return validate(field, value, inputOptions);
28
+ const validated = validate(field, value, inputOptions);
29
+ return validated.success && custom ? custom(validated.data) : validated;
29
30
  }
30
31
  return validator;
31
32
  };
@@ -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 type Definition = AnyDefinition | BooleanDefinition | NumberDefinition | StringDefinition | ArrayDefinition | TupleDefinition | StructDefinition | RecordDefinition;
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']>> : never;
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'> & {
@@ -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) {
@@ -22,23 +23,27 @@ export function getValidator(definition) {
22
23
  return getArrayValidator({
23
24
  // @ts-ignore 允许递归类型推断
24
25
  ...definition,
25
- item: getValidator(definition['item']),
26
+ item: getValidator(definition.item),
26
27
  });
27
28
  case 'tuple':
28
29
  return getTupleValidator({
29
30
  ...definition,
30
- tuple: definition['tuple'].map(def => getValidator(def)),
31
+ tuple: definition.tuple.map(def => getValidator(def)),
31
32
  });
32
33
  case 'struct': {
33
34
  const struct = {};
34
- for (const [key, def] of Object.entries(definition['struct']))
35
+ for (const [key, def] of Object.entries(definition.struct))
35
36
  struct[key] = getValidator(def);
36
37
  return getStructValidator({ ...definition, struct });
37
38
  }
38
39
  case 'record':
39
40
  return getRecordValidator({
40
41
  ...definition,
41
- record: getValidator(definition['record']),
42
+ record: getValidator(definition.record),
43
+ });
44
+ case 'oneOf':
45
+ return getOneOfValidator({
46
+ validators: definition.validators.map(def => getValidator(def)),
42
47
  });
43
48
  }
44
49
  }
@@ -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)
@@ -4,4 +4,5 @@ export * from './boolean.js';
4
4
  export * from './number.js';
5
5
  export * from './object.js';
6
6
  export * from './string.js';
7
+ export * from './oneOf.js';
7
8
  export * from './factory.js';
@@ -4,4 +4,5 @@ export * from './boolean.js';
4
4
  export * from './number.js';
5
5
  export * from './object.js';
6
6
  export * from './string.js';
7
+ export * from './oneOf.js';
7
8
  export * from './factory.js';
@@ -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
+ }