@kikiutils/mongoose 9.0.2 → 9.0.4

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 (73) hide show
  1. package/dist/assertions.js.map +1 -1
  2. package/dist/plugins/assertions.js +4 -4
  3. package/dist/plugins/assertions.js.map +1 -1
  4. package/dist/plugins/lean-decimal128-to-string.d.ts +6 -2
  5. package/dist/plugins/lean-decimal128-to-string.d.ts.map +1 -1
  6. package/dist/plugins/lean-decimal128-to-string.js +5 -4
  7. package/dist/plugins/lean-decimal128-to-string.js.map +1 -1
  8. package/dist/plugins/normalize.d.ts +3 -1
  9. package/dist/plugins/normalize.d.ts.map +1 -1
  10. package/dist/plugins/normalize.js +2 -1
  11. package/dist/plugins/normalize.js.map +1 -1
  12. package/dist/schema-builders/_internals.d.ts +18 -0
  13. package/dist/schema-builders/_internals.d.ts.map +1 -0
  14. package/dist/schema-builders/_internals.js +36 -0
  15. package/dist/schema-builders/_internals.js.map +1 -0
  16. package/dist/schema-builders/bigint.d.ts +5 -25
  17. package/dist/schema-builders/bigint.d.ts.map +1 -1
  18. package/dist/schema-builders/bigint.js +9 -19
  19. package/dist/schema-builders/bigint.js.map +1 -1
  20. package/dist/schema-builders/date.d.ts +4 -9
  21. package/dist/schema-builders/date.d.ts.map +1 -1
  22. package/dist/schema-builders/date.js.map +1 -1
  23. package/dist/schema-builders/decimal128.d.ts +6 -24
  24. package/dist/schema-builders/decimal128.d.ts.map +1 -1
  25. package/dist/schema-builders/decimal128.js +4 -6
  26. package/dist/schema-builders/decimal128.js.map +1 -1
  27. package/dist/schema-builders/double.d.ts +5 -25
  28. package/dist/schema-builders/double.d.ts.map +1 -1
  29. package/dist/schema-builders/double.js +9 -19
  30. package/dist/schema-builders/double.js.map +1 -1
  31. package/dist/schema-builders/int32.d.ts +5 -25
  32. package/dist/schema-builders/int32.d.ts.map +1 -1
  33. package/dist/schema-builders/int32.js +9 -19
  34. package/dist/schema-builders/int32.js.map +1 -1
  35. package/dist/schema-builders/number.d.ts +4 -9
  36. package/dist/schema-builders/number.d.ts.map +1 -1
  37. package/dist/schema-builders/number.js.map +1 -1
  38. package/dist/schema-builders/object-id.d.ts +2 -7
  39. package/dist/schema-builders/object-id.d.ts.map +1 -1
  40. package/dist/schema-builders/object-id.js.map +1 -1
  41. package/dist/schema-builders/ref.d.ts +2 -7
  42. package/dist/schema-builders/ref.d.ts.map +1 -1
  43. package/dist/schema-builders/ref.js.map +1 -1
  44. package/dist/schema-builders/string.d.ts +5 -10
  45. package/dist/schema-builders/string.d.ts.map +1 -1
  46. package/dist/schema-builders/string.js.map +1 -1
  47. package/dist/types/_internals.d.ts +22 -0
  48. package/dist/types/_internals.d.ts.map +1 -1
  49. package/dist/types/aggregate-paginate.d.ts +2 -1
  50. package/dist/types/aggregate-paginate.d.ts.map +1 -1
  51. package/dist/types/data.d.ts +3 -1
  52. package/dist/types/data.d.ts.map +1 -1
  53. package/dist/types/options.d.ts +6 -2
  54. package/dist/types/options.d.ts.map +1 -1
  55. package/package.json +23 -29
  56. package/src/assertions.ts +0 -1
  57. package/src/plugins/assertions.ts +4 -4
  58. package/src/plugins/lean-decimal128-to-string.ts +18 -7
  59. package/src/plugins/normalize.ts +5 -2
  60. package/src/schema-builders/_internals.ts +64 -0
  61. package/src/schema-builders/bigint.ts +33 -67
  62. package/src/schema-builders/date.ts +7 -7
  63. package/src/schema-builders/decimal128.ts +25 -41
  64. package/src/schema-builders/double.ts +35 -67
  65. package/src/schema-builders/int32.ts +33 -67
  66. package/src/schema-builders/number.ts +7 -7
  67. package/src/schema-builders/object-id.ts +2 -5
  68. package/src/schema-builders/ref.ts +2 -5
  69. package/src/schema-builders/string.ts +8 -8
  70. package/src/types/_internals.ts +26 -0
  71. package/src/types/aggregate-paginate.ts +2 -1
  72. package/src/types/data.ts +3 -1
  73. package/src/types/options.ts +6 -2
@@ -6,7 +6,10 @@ import type {
6
6
  } from 'mongoose';
7
7
  import type { Merge } from 'type-fest';
8
8
 
9
- import type { Readonlyable } from '../types/_internals';
9
+ import type {
10
+ EnumInput,
11
+ ValueWithMessage,
12
+ } from '../types/_internals';
10
13
 
11
14
  import { createBaseSchemaBuilderFactory } from './base';
12
15
 
@@ -35,10 +38,7 @@ export interface DateSchemaBuilder<
35
38
  >;
36
39
 
37
40
  enum: <
38
- T extends
39
- | Readonlyable<(D | null)[]>
40
- | { [path: string]: D | null }
41
- | { message?: M; values: Readonlyable<(D | null)[]> },
41
+ T extends EnumInput<D | null, M>,
42
42
  D extends NativeDate,
43
43
  M extends string,
44
44
  >(value: T) => ExtendSchemaBuilder<
@@ -53,7 +53,7 @@ export interface DateSchemaBuilder<
53
53
  >;
54
54
 
55
55
  max: <
56
- T extends D | Readonlyable<[D, S]>,
56
+ T extends ValueWithMessage<D, S>,
57
57
  D extends NativeDate,
58
58
  S extends string,
59
59
  >(value: T) => ExtendSchemaBuilder<
@@ -62,7 +62,7 @@ export interface DateSchemaBuilder<
62
62
  >;
63
63
 
64
64
  min: <
65
- T extends D | Readonlyable<[D, S]>,
65
+ T extends ValueWithMessage<D, S>,
66
66
  D extends NativeDate,
67
67
  S extends string,
68
68
  >(value: T) => ExtendSchemaBuilder<
@@ -8,11 +8,23 @@ import type {
8
8
  } from 'mongoose';
9
9
  import type { Merge } from 'type-fest';
10
10
 
11
- import type { Readonlyable } from '../types/_internals';
12
-
11
+ import type {
12
+ EnumInput,
13
+ LimitValidator,
14
+ ValidationSchema,
15
+ ValueWithMessage,
16
+ } from '../types/_internals';
17
+
18
+ import {
19
+ defaultLimitValidateMessages,
20
+ normalizeValueWithMessage,
21
+ setSchemaValidator,
22
+ } from './_internals';
13
23
  import { createBaseSchemaBuilderFactory } from './base';
14
24
 
15
25
  type Decimal128Limit = Decimal.Value | Types.Decimal128 | { toString: () => string };
26
+ type Decimal128ValidationSchema = ValidationSchema<LimitValidator<string, Types.Decimal128>>;
27
+
16
28
  type ExtendSchemaBuilder<
17
29
  Props extends BaseProps,
18
30
  ExtraOmitFields extends string,
@@ -25,16 +37,8 @@ interface BaseProps {
25
37
  type: typeof Schema.Types.Decimal128;
26
38
  }
27
39
 
28
- interface Decimal128LimitValidator {
29
- max?: string;
30
- message: string;
31
- min?: string;
32
- type: 'max' | 'min';
33
- validator: (value: null | Types.Decimal128 | undefined) => boolean;
34
- }
35
-
36
40
  export interface Decimal128SchemaBuilder<
37
- Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },
41
+ Props extends BaseProps = BaseProps,
38
42
  ExtraOmitFields extends string = never,
39
43
  > {
40
44
  default: <
@@ -46,10 +50,7 @@ export interface Decimal128SchemaBuilder<
46
50
  >;
47
51
 
48
52
  enum: <
49
- T extends
50
- | Readonlyable<(D | null)[]>
51
- | { [path: string]: D | null }
52
- | { message?: M; values: Readonlyable<(D | null)[]> },
53
+ T extends EnumInput<D | null, M>,
53
54
  D extends Types.Decimal128,
54
55
  M extends string,
55
56
  >(value: T) => ExtendSchemaBuilder<
@@ -64,7 +65,7 @@ export interface Decimal128SchemaBuilder<
64
65
  >;
65
66
 
66
67
  max: <
67
- T extends D | Readonlyable<[D, S]>,
68
+ T extends ValueWithMessage<D, S>,
68
69
  D extends Decimal128Limit,
69
70
  S extends string,
70
71
  >(value: T) => ExtendSchemaBuilder<
@@ -73,7 +74,7 @@ export interface Decimal128SchemaBuilder<
73
74
  >;
74
75
 
75
76
  min: <
76
- T extends D | Readonlyable<[D, S]>,
77
+ T extends ValueWithMessage<D, S>,
77
78
  D extends Decimal128Limit,
78
79
  S extends string,
79
80
  >(value: T) => ExtendSchemaBuilder<
@@ -119,10 +120,6 @@ export interface Decimal128SchemaBuilder<
119
120
  unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
120
121
  }
121
122
 
122
- interface Decimal128ValidationSchema {
123
- validate: Decimal128LimitValidator[];
124
- }
125
-
126
123
  interface ToStringGetterSchema {
127
124
  get: (value?: Types.Decimal128) => string | undefined;
128
125
  leanDecimal128ToString: true;
@@ -132,29 +129,20 @@ interface ToStringSetterSchema {
132
129
  set: (value?: { toString: () => string }) => string | undefined;
133
130
  }
134
131
 
135
- // Constants/Variables
136
- const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
137
- const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
138
-
139
132
  // Functions
140
133
  const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
141
134
 
142
135
  function createLimitValidator(
143
136
  type: 'max' | 'min',
144
- value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>,
145
- ): Decimal128LimitValidator {
146
- const [limit, message] = Array.isArray(value)
147
- ? value as Readonly<[Decimal128Limit, string]>
148
- : [
149
- value,
150
- undefined,
151
- ];
137
+ value: ValueWithMessage<Decimal128Limit>,
138
+ ): LimitValidator<string, Types.Decimal128> {
139
+ const [limit, message] = normalizeValueWithMessage(value);
152
140
 
153
141
  const decimalLimit = new Decimal(limit.toString());
154
142
  if (!decimalLimit.isFinite()) throw new RangeError(`Decimal128 ${type} limit must be finite`);
155
143
 
156
144
  return {
157
- message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
145
+ message: message ?? defaultLimitValidateMessages[type],
158
146
  [type]: decimalLimit.toString(),
159
147
  type,
160
148
  validator: (value) => {
@@ -167,20 +155,16 @@ function createLimitValidator(
167
155
  }
168
156
 
169
157
  export function decimal128SchemaBuilder() {
170
- const schema: Record<string, any> = {};
158
+ const schema: Partial<Decimal128ValidationSchema> & Record<string, any> = {};
171
159
  const baseBuilder = baseBuilderFactory(schema);
172
160
  return new Proxy(
173
161
  baseBuilder,
174
162
  {
175
163
  get(target, key, receiver) {
176
164
  if (key === 'max' || key === 'min') {
177
- return (value: Decimal128Limit | Readonlyable<[Decimal128Limit, string]>) => {
165
+ return (value: ValueWithMessage<Decimal128Limit>) => {
178
166
  const validator = createLimitValidator(key, value);
179
- const validators = Array.isArray(schema.validate) ? schema.validate : [];
180
- schema.validate = [
181
- ...validators.filter(({ type }) => type !== key),
182
- validator,
183
- ];
167
+ setSchemaValidator(schema, validator);
184
168
 
185
169
  return receiver;
186
170
  };
@@ -6,11 +6,29 @@ import type {
6
6
  } from 'mongoose';
7
7
  import type { Merge } from 'type-fest';
8
8
 
9
- import type { Readonlyable } from '../types/_internals';
10
-
9
+ import type {
10
+ EnumInput,
11
+ EnumValidator,
12
+ LimitValidator,
13
+ ValidationSchema,
14
+ ValueWithMessage,
15
+ } from '../types/_internals';
16
+
17
+ import {
18
+ defaultEnumValidateMessage,
19
+ defaultLimitValidateMessages,
20
+ hasSchemaValidator,
21
+ normalizeEnumValues,
22
+ normalizeValueWithMessage,
23
+ setSchemaValidator,
24
+ } from './_internals';
11
25
  import { createBaseSchemaBuilderFactory } from './base';
12
26
 
13
27
  type DoubleLimit = number | Types.Double;
28
+ type DoubleValidationSchema = ValidationSchema<
29
+ EnumValidator<number | Types.Double, Types.Double> | LimitValidator<number, Types.Double>
30
+ >;
31
+
14
32
  type ExtendSchemaBuilder<
15
33
  Props extends BaseProps,
16
34
  ExtraOmitFields extends string,
@@ -23,21 +41,6 @@ interface BaseProps {
23
41
  type: typeof Schema.Types.Double;
24
42
  }
25
43
 
26
- interface DoubleEnumValidator {
27
- enumValues: (null | number | Types.Double)[];
28
- message: string;
29
- type: 'enum';
30
- validator: (value: null | Types.Double | undefined) => boolean;
31
- }
32
-
33
- interface DoubleLimitValidator {
34
- max?: number;
35
- message: string;
36
- min?: number;
37
- type: 'max' | 'min';
38
- validator: (value: null | Types.Double | undefined) => boolean;
39
- }
40
-
41
44
  export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
42
45
  default: <
43
46
  T extends ((this: any, doc: any) => D | null | undefined) | D | null | undefined,
@@ -48,10 +51,7 @@ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
48
51
  >;
49
52
 
50
53
  enum: <
51
- T extends
52
- | Readonlyable<(N | null)[]>
53
- | { [path: string]: N | null }
54
- | { message?: M; values: Readonlyable<(N | null)[]> },
54
+ T extends EnumInput<N | null, M>,
55
55
  M extends string,
56
56
  N extends number | Types.Double,
57
57
  >(value: T) => ExtendSchemaBuilder<
@@ -66,7 +66,7 @@ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
66
66
  >;
67
67
 
68
68
  max: <
69
- T extends L | Readonlyable<[L, S]>,
69
+ T extends ValueWithMessage<L, S>,
70
70
  L extends DoubleLimit,
71
71
  S extends string,
72
72
  >(value: T) => ExtendSchemaBuilder<
@@ -75,7 +75,7 @@ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
75
75
  >;
76
76
 
77
77
  min: <
78
- T extends L | Readonlyable<[L, S]>,
78
+ T extends ValueWithMessage<L, S>,
79
79
  L extends DoubleLimit,
80
80
  S extends string,
81
81
  >(value: T) => ExtendSchemaBuilder<
@@ -90,34 +90,20 @@ export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraO
90
90
  unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
91
91
  }
92
92
 
93
- interface DoubleValidationSchema {
94
- validate: (DoubleEnumValidator | DoubleLimitValidator)[];
95
- }
96
-
97
- // Constants/Variables
98
- const defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';
99
- const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
100
- const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
101
-
102
93
  // Functions
103
94
  const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Double);
104
95
 
105
96
  function createLimitValidator(
106
97
  type: 'max' | 'min',
107
- value: DoubleLimit | Readonlyable<[DoubleLimit, string]>,
108
- ): DoubleLimitValidator {
109
- const [limit, message] = Array.isArray(value)
110
- ? value as Readonly<[DoubleLimit, string]>
111
- : [
112
- value as DoubleLimit,
113
- undefined,
114
- ];
98
+ value: ValueWithMessage<DoubleLimit>,
99
+ ): LimitValidator<number, Types.Double> {
100
+ const [limit, message] = normalizeValueWithMessage(value);
115
101
 
116
102
  const numericLimit = typeof limit === 'number' ? limit : limit.valueOf();
117
103
  if (!Number.isFinite(numericLimit)) throw new RangeError(`Double ${type} limit must be finite`);
118
104
 
119
105
  return {
120
- message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
106
+ message: message ?? defaultLimitValidateMessages[type],
121
107
  [type]: numericLimit,
122
108
  type,
123
109
  validator: (value) => {
@@ -129,36 +115,25 @@ function createLimitValidator(
129
115
  }
130
116
 
131
117
  export function doubleSchemaBuilder() {
132
- const schema: Record<string, any> = {};
118
+ const schema: Partial<DoubleValidationSchema> & Record<string, any> = {};
133
119
  const baseBuilder = baseBuilderFactory(schema);
134
120
  return new Proxy(
135
121
  baseBuilder,
136
122
  {
137
123
  get(target, key, receiver) {
138
124
  if (key === 'enum') {
139
- if (schema.validate?.some(({ type }: { type: string }) => type === 'enum')) {
125
+ if (hasSchemaValidator(schema, 'enum')) {
140
126
  throw new Error('Duplicate schema attribute: enum');
141
127
  }
142
128
 
143
- return (
144
- value: Readonlyable<(null | number | Types.Double)[]>
145
- | { [path: string]: null | number | Types.Double }
146
- | { message?: string; values: Readonlyable<(null | number | Types.Double)[]> },
147
- ) => {
148
- let enumValues: (null | number | Types.Double)[];
149
- let message: string | undefined;
150
-
151
- if (Array.isArray(value)) enumValues = [...value];
152
- else if ('values' in value && Array.isArray(value.values)) {
153
- enumValues = [...value.values];
154
- if ('message' in value && typeof value.message === 'string') message = value.message;
155
- } else enumValues = Object.values(value);
129
+ return (value: EnumInput<null | number | Types.Double>) => {
130
+ const { enumValues, message } = normalizeEnumValues(value);
156
131
 
157
132
  const allowedValues = new Set(
158
133
  enumValues.map((value) => value === null ? null : value.valueOf()),
159
134
  );
160
135
 
161
- const validator: DoubleEnumValidator = {
136
+ const validator: EnumValidator<number | Types.Double, Types.Double> = {
162
137
  enumValues,
163
138
  message: message ?? defaultEnumValidateMessage,
164
139
  type: 'enum',
@@ -169,23 +144,16 @@ export function doubleSchemaBuilder() {
169
144
  },
170
145
  };
171
146
 
172
- schema.validate = [
173
- ...schema.validate ?? [],
174
- validator,
175
- ];
147
+ setSchemaValidator(schema, validator);
176
148
 
177
149
  return receiver;
178
150
  };
179
151
  }
180
152
 
181
153
  if (key === 'max' || key === 'min') {
182
- return (value: DoubleLimit | Readonlyable<[DoubleLimit, string]>) => {
154
+ return (value: ValueWithMessage<DoubleLimit>) => {
183
155
  const validator = createLimitValidator(key, value);
184
- const validators = Array.isArray(schema.validate) ? schema.validate : [];
185
- schema.validate = [
186
- ...validators.filter(({ type }) => type !== key),
187
- validator,
188
- ];
156
+ setSchemaValidator(schema, validator);
189
157
 
190
158
  return receiver;
191
159
  };
@@ -6,8 +6,22 @@ import type {
6
6
  } from 'mongoose';
7
7
  import type { Merge } from 'type-fest';
8
8
 
9
- import type { Readonlyable } from '../types/_internals';
10
-
9
+ import type {
10
+ EnumInput,
11
+ EnumValidator,
12
+ LimitValidator,
13
+ ValidationSchema,
14
+ ValueWithMessage,
15
+ } from '../types/_internals';
16
+
17
+ import {
18
+ defaultEnumValidateMessage,
19
+ defaultLimitValidateMessages,
20
+ hasSchemaValidator,
21
+ normalizeEnumValues,
22
+ normalizeValueWithMessage,
23
+ setSchemaValidator,
24
+ } from './_internals';
11
25
  import { createBaseSchemaBuilderFactory } from './base';
12
26
 
13
27
  type ExtendSchemaBuilder<
@@ -20,25 +34,12 @@ type ExtendSchemaBuilder<
20
34
 
21
35
  type Int32Limit = number;
22
36
 
37
+ type Int32ValidationSchema = ValidationSchema<EnumValidator<number> | LimitValidator<number>>;
38
+
23
39
  interface BaseProps {
24
40
  type: typeof Schema.Types.Int32;
25
41
  }
26
42
 
27
- interface Int32EnumValidator {
28
- enumValues: (null | number)[];
29
- message: string;
30
- type: 'enum';
31
- validator: (value: null | number | undefined) => boolean;
32
- }
33
-
34
- interface Int32LimitValidator {
35
- max?: number;
36
- message: string;
37
- min?: number;
38
- type: 'max' | 'min';
39
- validator: (value: null | number | undefined) => boolean;
40
- }
41
-
42
43
  export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
43
44
  default: <
44
45
  T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,
@@ -49,10 +50,7 @@ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOm
49
50
  >;
50
51
 
51
52
  enum: <
52
- T extends
53
- | Readonlyable<(N | null)[]>
54
- | { [path: string]: N | null }
55
- | { message?: M; values: Readonlyable<(N | null)[]> },
53
+ T extends EnumInput<N | null, M>,
56
54
  M extends string,
57
55
  N extends number,
58
56
  >(value: T) => ExtendSchemaBuilder<
@@ -67,7 +65,7 @@ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOm
67
65
  >;
68
66
 
69
67
  max: <
70
- T extends L | Readonlyable<[L, S]>,
68
+ T extends ValueWithMessage<L, S>,
71
69
  L extends Int32Limit,
72
70
  S extends string,
73
71
  >(value: T) => ExtendSchemaBuilder<
@@ -76,7 +74,7 @@ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOm
76
74
  >;
77
75
 
78
76
  min: <
79
- T extends L | Readonlyable<[L, S]>,
77
+ T extends ValueWithMessage<L, S>,
80
78
  L extends Int32Limit,
81
79
  S extends string,
82
80
  >(value: T) => ExtendSchemaBuilder<
@@ -91,33 +89,19 @@ export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOm
91
89
  unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
92
90
  }
93
91
 
94
- interface Int32ValidationSchema {
95
- validate: (Int32EnumValidator | Int32LimitValidator)[];
96
- }
97
-
98
- // Constants/Variables
99
- const defaultEnumValidateMessage = 'Path `{PATH}` ({VALUE}) is not a valid enum value.';
100
- const defaultMinValidateMessage = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
101
- const defaultMaxValidateMessage = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
102
-
103
92
  // Functions
104
93
  const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Int32);
105
94
 
106
95
  function createLimitValidator(
107
96
  type: 'max' | 'min',
108
- value: Int32Limit | Readonlyable<[Int32Limit, string]>,
109
- ): Int32LimitValidator {
110
- const [numericLimit, message] = Array.isArray(value)
111
- ? value as Readonly<[Int32Limit, string]>
112
- : [
113
- value as Int32Limit,
114
- undefined,
115
- ];
97
+ value: ValueWithMessage<Int32Limit>,
98
+ ): LimitValidator<number> {
99
+ const [numericLimit, message] = normalizeValueWithMessage(value);
116
100
 
117
101
  if (!Number.isFinite(numericLimit)) throw new RangeError(`Int32 ${type} limit must be finite`);
118
102
 
119
103
  return {
120
- message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
104
+ message: message ?? defaultLimitValidateMessages[type],
121
105
  [type]: numericLimit,
122
106
  type,
123
107
  validator: (value) => {
@@ -129,56 +113,38 @@ function createLimitValidator(
129
113
  }
130
114
 
131
115
  export function int32SchemaBuilder() {
132
- const schema: Record<string, any> = {};
116
+ const schema: Partial<Int32ValidationSchema> & Record<string, any> = {};
133
117
  const baseBuilder = baseBuilderFactory(schema);
134
118
  return new Proxy(
135
119
  baseBuilder,
136
120
  {
137
121
  get(target, key, receiver) {
138
122
  if (key === 'enum') {
139
- if (schema.validate?.some(({ type }: { type: string }) => type === 'enum')) {
123
+ if (hasSchemaValidator(schema, 'enum')) {
140
124
  throw new Error('Duplicate schema attribute: enum');
141
125
  }
142
126
 
143
- return (
144
- value: Readonlyable<(null | number)[]>
145
- | { [path: string]: null | number }
146
- | { message?: string; values: Readonlyable<(null | number)[]> },
147
- ) => {
148
- let enumValues: (null | number)[];
149
- let message: string | undefined;
150
-
151
- if (Array.isArray(value)) enumValues = [...value];
152
- else if ('values' in value && Array.isArray(value.values)) {
153
- enumValues = [...value.values];
154
- if ('message' in value && typeof value.message === 'string') message = value.message;
155
- } else enumValues = Object.values(value);
127
+ return (value: EnumInput<null | number>) => {
128
+ const { enumValues, message } = normalizeEnumValues(value);
156
129
 
157
130
  const allowedValues = new Set(enumValues);
158
- const validator: Int32EnumValidator = {
131
+ const validator: EnumValidator<number> = {
159
132
  enumValues,
160
133
  message: message ?? defaultEnumValidateMessage,
161
134
  type: 'enum',
162
135
  validator: (value) => value === null || value === undefined || allowedValues.has(value),
163
136
  };
164
137
 
165
- schema.validate = [
166
- ...schema.validate ?? [],
167
- validator,
168
- ];
138
+ setSchemaValidator(schema, validator);
169
139
 
170
140
  return receiver;
171
141
  };
172
142
  }
173
143
 
174
144
  if (key === 'max' || key === 'min') {
175
- return (value: Int32Limit | Readonlyable<[Int32Limit, string]>) => {
145
+ return (value: ValueWithMessage<Int32Limit>) => {
176
146
  const validator = createLimitValidator(key, value);
177
- const validators = Array.isArray(schema.validate) ? schema.validate : [];
178
- schema.validate = [
179
- ...validators.filter(({ type }) => type !== key),
180
- validator,
181
- ];
147
+ setSchemaValidator(schema, validator);
182
148
 
183
149
  return receiver;
184
150
  };
@@ -6,7 +6,10 @@ import type {
6
6
  } from 'mongoose';
7
7
  import type { Merge } from 'type-fest';
8
8
 
9
- import type { Readonlyable } from '../types/_internals';
9
+ import type {
10
+ EnumInput,
11
+ ValueWithMessage,
12
+ } from '../types/_internals';
10
13
 
11
14
  import { createBaseSchemaBuilderFactory } from './base';
12
15
 
@@ -35,10 +38,7 @@ export interface NumberSchemaBuilder<
35
38
  >;
36
39
 
37
40
  enum: <
38
- T extends
39
- | Readonlyable<(N | null)[]>
40
- | { [path: string]: N | null }
41
- | { message?: M; values: Readonlyable<(N | null)[]> },
41
+ T extends EnumInput<N | null, M>,
42
42
  M extends string,
43
43
  N extends number,
44
44
  >(value: T) => ExtendSchemaBuilder<
@@ -53,7 +53,7 @@ export interface NumberSchemaBuilder<
53
53
  >;
54
54
 
55
55
  max: <
56
- T extends N | Readonlyable<[N, S]>,
56
+ T extends ValueWithMessage<N, S>,
57
57
  N extends number,
58
58
  S extends string,
59
59
  >(value: T) => ExtendSchemaBuilder<
@@ -62,7 +62,7 @@ export interface NumberSchemaBuilder<
62
62
  >;
63
63
 
64
64
  min: <
65
- T extends N | Readonlyable<[N, S]>,
65
+ T extends ValueWithMessage<N, S>,
66
66
  N extends number,
67
67
  S extends string,
68
68
  >(value: T) => ExtendSchemaBuilder<
@@ -7,7 +7,7 @@ import type {
7
7
  } from 'mongoose';
8
8
  import type { Merge } from 'type-fest';
9
9
 
10
- import type { Readonlyable } from '../types/_internals';
10
+ import type { EnumInput } from '../types/_internals';
11
11
 
12
12
  import { createBaseSchemaBuilderFactory } from './base';
13
13
 
@@ -36,10 +36,7 @@ export interface ObjectIdSchemaBuilder<
36
36
  >;
37
37
 
38
38
  enum: <
39
- T extends
40
- | Readonlyable<(null | O)[]>
41
- | { [path: string]: null | O }
42
- | { message?: M; values: Readonlyable<(null | O)[]> },
39
+ T extends EnumInput<null | O, M>,
43
40
  M extends string,
44
41
  O extends Types.ObjectId,
45
42
  >(value: T) => ExtendSchemaBuilder<
@@ -8,7 +8,7 @@ import type {
8
8
  } from 'mongoose';
9
9
  import type { Merge } from 'type-fest';
10
10
 
11
- import type { Readonlyable } from '../types/_internals';
11
+ import type { EnumInput } from '../types/_internals';
12
12
 
13
13
  import { createBaseSchemaBuilderFactory } from './base';
14
14
 
@@ -37,10 +37,7 @@ export interface RefSchemaBuilder<
37
37
  >;
38
38
 
39
39
  enum: <
40
- T extends
41
- | Readonlyable<(null | O)[]>
42
- | { [path: string]: null | O }
43
- | { message?: M; values: Readonlyable<(null | O)[]> },
40
+ T extends EnumInput<null | O, M>,
44
41
  M extends string,
45
42
  O extends Types.ObjectId,
46
43
  >(value: T) => ExtendSchemaBuilder<