@anjianshi/utils 2.4.10 → 2.4.11

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.11",
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-react": "4.0.13",
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-typescript": "5.0.11"
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
  };
@@ -22,23 +22,23 @@ export function getValidator(definition) {
22
22
  return getArrayValidator({
23
23
  // @ts-ignore 允许递归类型推断
24
24
  ...definition,
25
- item: getValidator(definition['item']),
25
+ item: getValidator(definition.item),
26
26
  });
27
27
  case 'tuple':
28
28
  return getTupleValidator({
29
29
  ...definition,
30
- tuple: definition['tuple'].map(def => getValidator(def)),
30
+ tuple: definition.tuple.map(def => getValidator(def)),
31
31
  });
32
32
  case 'struct': {
33
33
  const struct = {};
34
- for (const [key, def] of Object.entries(definition['struct']))
34
+ for (const [key, def] of Object.entries(definition.struct))
35
35
  struct[key] = getValidator(def);
36
36
  return getStructValidator({ ...definition, struct });
37
37
  }
38
38
  case 'record':
39
39
  return getRecordValidator({
40
40
  ...definition,
41
- record: getValidator(definition['record']),
41
+ record: getValidator(definition.record),
42
42
  });
43
43
  }
44
44
  }