@anjianshi/utils 1.2.6 → 1.3.0

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.
Files changed (57) hide show
  1. package/env-node/env-reader.d.ts +12 -0
  2. package/env-node/env-reader.js +31 -0
  3. package/env-node/{logging.d.ts → logging/handlers.d.ts} +1 -6
  4. package/env-node/{logging.js → logging/handlers.js} +1 -12
  5. package/env-node/logging/index.d.ts +11 -0
  6. package/env-node/logging/index.js +14 -0
  7. package/env-node/typeorm/adapt-logging.d.ts +11 -0
  8. package/env-node/typeorm/adapt-logging.js +42 -0
  9. package/env-node/typeorm/index.d.ts +31 -0
  10. package/env-node/typeorm/index.js +39 -0
  11. package/lang/index.d.ts +1 -0
  12. package/lang/index.js +1 -0
  13. package/lang/may-success.d.ts +40 -0
  14. package/lang/may-success.js +27 -0
  15. package/lang/types.d.ts +12 -43
  16. package/lang/types.js +0 -13
  17. package/logging/index.js +1 -1
  18. package/md5.d.ts +30 -0
  19. package/md5.js +309 -0
  20. package/package.json +3 -1
  21. package/src/env-node/env-reader.ts +33 -0
  22. package/src/env-node/{logging.ts → logging/handlers.ts} +1 -21
  23. package/src/env-node/logging/index.ts +16 -0
  24. package/src/env-node/typeorm/adapt-logging.ts +54 -0
  25. package/src/env-node/typeorm/index.ts +62 -0
  26. package/src/lang/index.ts +1 -0
  27. package/src/lang/may-success.ts +57 -0
  28. package/src/lang/types.ts +14 -59
  29. package/src/logging/index.ts +1 -1
  30. package/src/md5.ts +319 -0
  31. package/src/url.ts +48 -55
  32. package/src/validators/array.ts +62 -0
  33. package/src/validators/base.ts +49 -0
  34. package/src/validators/boolean.ts +24 -0
  35. package/src/validators/factories.ts +47 -0
  36. package/src/validators/index.ts +9 -0
  37. package/src/validators/number.ts +43 -0
  38. package/src/validators/object.ts +70 -0
  39. package/src/validators/string.ts +55 -0
  40. package/url.d.ts +31 -15
  41. package/url.js +22 -28
  42. package/validators/array.d.ts +20 -0
  43. package/validators/array.js +44 -0
  44. package/validators/base.d.ts +26 -0
  45. package/validators/base.js +28 -0
  46. package/validators/boolean.d.ts +4 -0
  47. package/validators/boolean.js +28 -0
  48. package/validators/factories.d.ts +18 -0
  49. package/validators/factories.js +41 -0
  50. package/validators/index.d.ts +7 -0
  51. package/validators/index.js +7 -0
  52. package/validators/number.d.ts +15 -0
  53. package/validators/number.js +32 -0
  54. package/validators/object.d.ts +20 -0
  55. package/validators/object.js +52 -0
  56. package/validators/string.d.ts +17 -0
  57. package/validators/string.js +35 -0
@@ -0,0 +1,41 @@
1
+ /**
2
+ * 创建 validator 的快捷方式
3
+ */
4
+ import { ArrayValidator } from './array.js';
5
+ import { Validator } from './base.js';
6
+ import { BooleanValidator } from './boolean.js';
7
+ import { NumberValidator } from './number.js';
8
+ import { ObjectValidator } from './object.js';
9
+ import { StringValidator } from './string.js';
10
+ /** 仅进行基本检查(如检查空值),不检查具体格式 */
11
+ export function any(options) {
12
+ return new Validator(options ?? {});
13
+ }
14
+ export function string(...args) {
15
+ return new StringValidator(...args);
16
+ }
17
+ export function number(...args) {
18
+ return new NumberValidator(...args);
19
+ }
20
+ export function boolean(options = {}) {
21
+ return new BooleanValidator(options);
22
+ }
23
+ export function array(options) {
24
+ if (options instanceof Validator)
25
+ options = { item: options };
26
+ return new ArrayValidator(options);
27
+ }
28
+ export function tuple(validators, baseOptions) {
29
+ return new ArrayValidator({
30
+ ...(baseOptions ?? {}),
31
+ tuple: validators,
32
+ });
33
+ }
34
+ export function struct(validators, options = {}) {
35
+ return new ObjectValidator({ ...options, struct: validators });
36
+ }
37
+ export function record(options) {
38
+ if (options instanceof Validator)
39
+ options = { record: options };
40
+ return new ObjectValidator(options);
41
+ }
@@ -0,0 +1,7 @@
1
+ export * from './base.js';
2
+ export * from './array.js';
3
+ export * from './boolean.js';
4
+ export * from './number.js';
5
+ export * from './object.js';
6
+ export * from './string.js';
7
+ export * as validators from './factories.js';
@@ -0,0 +1,7 @@
1
+ export * from './base.js';
2
+ export * from './array.js';
3
+ export * from './boolean.js';
4
+ export * from './number.js';
5
+ export * from './object.js';
6
+ export * from './string.js';
7
+ export * as validators from './factories.js';
@@ -0,0 +1,15 @@
1
+ import { type BaseOptions, Validator } from './base.js';
2
+ export interface NumberOptions {
3
+ /** 数值最小值 */
4
+ min?: number;
5
+ /** 数值最大值 */
6
+ max?: number;
7
+ /** 是否允许小数 @default false */
8
+ float: boolean;
9
+ /** 指定可选值 */
10
+ enum?: number[];
11
+ }
12
+ export declare class NumberValidator extends Validator<NumberOptions> {
13
+ constructor(options?: Partial<BaseOptions & NumberOptions>);
14
+ validate(field: string, value: unknown): import("../lang/may-success.js").Failed<void> | import("../lang/may-success.js").Success<unknown>;
15
+ }
@@ -0,0 +1,32 @@
1
+ import { success, failed } from '../lang/index.js';
2
+ import { Validator } from './base.js';
3
+ export class NumberValidator extends Validator {
4
+ constructor(options = {}) {
5
+ super({
6
+ float: false,
7
+ ...options,
8
+ });
9
+ }
10
+ validate(field, value) {
11
+ const superResult = super.validate(field, value);
12
+ if (!superResult.success)
13
+ return superResult;
14
+ value = superResult.data;
15
+ if (value === null || value === undefined)
16
+ return superResult;
17
+ const opt = this.options;
18
+ if (typeof value === 'string')
19
+ value = parseFloat(value);
20
+ if (typeof value !== 'number' || !isFinite(value))
21
+ return failed(`${field} must be a valid number`);
22
+ if (opt.enum !== undefined && !opt.enum.includes(value))
23
+ return failed(`${field} can only be one of ${opt.enum.join(', ')}.`);
24
+ if (!opt.float && value % 1 !== 0)
25
+ return failed(`${field} must be a integer`);
26
+ if (typeof opt.min === 'number' && value < opt.min)
27
+ return failed(`${field} must >= ${opt.min}`);
28
+ if (typeof opt.max === 'number' && value > opt.max)
29
+ return failed(`${field} must <= ${opt.max}`);
30
+ return success(value);
31
+ }
32
+ }
@@ -0,0 +1,20 @@
1
+ import { Validator } from './base.js';
2
+ /** 验证有明确键值对结构的对象 */
3
+ export type StructOptions = {
4
+ /** 定义对象结构,及各个值的验证规则 */
5
+ struct: Record<string, Validator>;
6
+ };
7
+ /**
8
+ * 验证有任意多个 key,但值的类型固定的对象
9
+ */
10
+ export type RecordOptions = {
11
+ /** 验证单个值 */
12
+ record: Validator;
13
+ /** 对象至少要有几项 */
14
+ min?: number;
15
+ /** 对象最多有几项 */
16
+ max?: number;
17
+ };
18
+ export declare class ObjectValidator extends Validator<StructOptions | RecordOptions> {
19
+ validate(field: string, value: unknown): import("../lang/may-success.js").Failed<void> | import("../lang/may-success.js").Success<unknown>;
20
+ }
@@ -0,0 +1,52 @@
1
+ import isPlainObject from 'lodash/isPlainObject.js';
2
+ import { success, failed } from '../lang/index.js';
3
+ import { Validator } from './base.js';
4
+ export class ObjectValidator extends Validator {
5
+ validate(field, value) {
6
+ const superResult = super.validate(field, value);
7
+ if (!superResult.success)
8
+ return superResult;
9
+ value = superResult.data;
10
+ if (value === null || value === undefined)
11
+ return superResult;
12
+ const opt = this.options;
13
+ if (!isPlainObject(value))
14
+ return failed(`${field} should be a plain object`);
15
+ const formatted = {};
16
+ if ('struct' in opt) {
17
+ for (const [key, itemValidator] of Object.entries(opt.struct)) {
18
+ const itemResult = itemValidator.validate(`${field}["${key}"]`, value[key]);
19
+ if (itemResult.success) {
20
+ if (itemResult.data !== undefined)
21
+ formatted[key] = itemResult.data;
22
+ }
23
+ else {
24
+ return itemResult;
25
+ }
26
+ }
27
+ }
28
+ else {
29
+ for (const [key, itemValue] of Object.entries(value)) {
30
+ // record 场景下,值为 undefined 的项目视为不存在,不保留在验证结果里,
31
+ // 不然一些因为不想赋值而填充了 undefined 值的项目可能意外触发验证失败,或意外得到了默认值。
32
+ // (因此 validator 的 required 选项和 defaults 选项也没有意义了)
33
+ if (itemValue === undefined)
34
+ continue;
35
+ const itemResult = opt.record.validate(`${field}["${key}"]`, itemValue);
36
+ if (itemResult.success) {
37
+ if (itemResult.data !== undefined)
38
+ formatted[key] = itemResult.data;
39
+ }
40
+ else {
41
+ return itemResult;
42
+ }
43
+ }
44
+ const length = Object.keys(formatted).length;
45
+ if (typeof opt.min === 'number' && length < opt.min)
46
+ return failed(`size of ${field} should >= ${opt.min}`);
47
+ if (typeof opt.max === 'number' && length > opt.max)
48
+ return failed(`size of ${field} should <= ${opt.max}`);
49
+ }
50
+ return success(formatted);
51
+ }
52
+ }
@@ -0,0 +1,17 @@
1
+ import { type BaseOptions, Validator } from './base.js';
2
+ export interface StringOptions {
3
+ /** 字符串最小长度。defaults='' 时默认为 0,否则默认为 1 */
4
+ min: number;
5
+ /** 字符串最大长度。 */
6
+ max?: number;
7
+ /** 字符串需匹配此正则 */
8
+ pattern?: RegExp;
9
+ /** 指定一个数组或 TypeScript enum,字段值必须在此 enum 之中 */
10
+ enum?: string[] | Record<string, string>;
11
+ /** 验证之前,是否先清除两侧空白字符(默认开启) */
12
+ trim: boolean;
13
+ }
14
+ export declare class StringValidator extends Validator<StringOptions> {
15
+ constructor(options?: Partial<BaseOptions & StringOptions>);
16
+ validate(field: string, value: unknown): import("../lang/may-success.js").Failed<void> | import("../lang/may-success.js").Success<unknown>;
17
+ }
@@ -0,0 +1,35 @@
1
+ import { success, failed } from '../lang/index.js';
2
+ import { Validator } from './base.js';
3
+ export class StringValidator extends Validator {
4
+ constructor(options = {}) {
5
+ super({
6
+ min: options.defaults === '' ? 0 : 1,
7
+ trim: true,
8
+ ...options,
9
+ });
10
+ }
11
+ validate(field, value) {
12
+ const opt = this.options;
13
+ const superResult = super.validate(field, value);
14
+ if (!superResult.success)
15
+ return superResult;
16
+ value = superResult.data;
17
+ if (value === null || value === undefined)
18
+ return superResult;
19
+ if (typeof value !== 'string')
20
+ return failed(`${field} must be a string`);
21
+ const formatted = opt.trim ? value.trim() : value;
22
+ if (typeof opt.min === 'number' && formatted.length < opt.min)
23
+ return failed(`${field}'s length must >= ${opt.min}`);
24
+ if (typeof opt.max === 'number' && formatted.length > opt.max)
25
+ return failed(`${field}'s length must <= ${opt.max}`);
26
+ if (opt.pattern && !opt.pattern.exec(formatted))
27
+ return failed(`${field} does not match the pattern.`);
28
+ if (opt.enum !== undefined) {
29
+ const validValues = Array.isArray(opt.enum) ? opt.enum : Object.values(opt.enum);
30
+ if (!validValues.includes(formatted))
31
+ return failed(`${field} can only be one of ${validValues.join(', ')}.`);
32
+ }
33
+ return success(formatted);
34
+ }
35
+ }