@kikiutils/mongoose 7.1.0 → 9.0.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.
- package/README.md +8 -8
- package/dist/_internals.d.ts +4 -0
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js +2 -1
- package/dist/_internals.js.map +1 -1
- package/dist/builders.d.ts +1 -3
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +8 -9
- package/dist/builders.js.map +1 -1
- package/dist/connection.d.ts +5 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +10 -0
- package/dist/connection.js.map +1 -0
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +2 -1
- package/dist/options.js.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.d.ts +26 -0
- package/dist/plugins/lean-decimal128-to-string.d.ts.map +1 -0
- package/dist/plugins/lean-decimal128-to-string.js +85 -0
- package/dist/plugins/lean-decimal128-to-string.js.map +1 -0
- package/dist/plugins/normalize.d.ts +24 -14
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +20 -10
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/base.d.ts +1 -1
- package/dist/schema-builders/base.d.ts.map +1 -1
- package/dist/schema-builders/base.js.map +1 -1
- package/dist/schema-builders/bigint.d.ts +60 -0
- package/dist/schema-builders/bigint.d.ts.map +1 -0
- package/dist/schema-builders/bigint.js +58 -0
- package/dist/schema-builders/bigint.js.map +1 -0
- package/dist/schema-builders/boolean.d.ts.map +1 -1
- package/dist/schema-builders/boolean.js.map +1 -1
- package/dist/schema-builders/date.d.ts +2 -2
- package/dist/schema-builders/date.d.ts.map +1 -1
- package/dist/schema-builders/date.js.map +1 -1
- package/dist/schema-builders/decimal128.d.ts +20 -14
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.js +1 -0
- package/dist/schema-builders/decimal128.js.map +1 -1
- package/dist/schema-builders/double.d.ts +60 -0
- package/dist/schema-builders/double.d.ts.map +1 -0
- package/dist/schema-builders/double.js +62 -0
- package/dist/schema-builders/double.js.map +1 -0
- package/dist/schema-builders/index.d.ts +6 -1
- package/dist/schema-builders/index.js +6 -1
- package/dist/schema-builders/int32.d.ts +60 -0
- package/dist/schema-builders/int32.d.ts.map +1 -0
- package/dist/schema-builders/int32.js +58 -0
- package/dist/schema-builders/int32.js.map +1 -0
- package/dist/schema-builders/mixed.d.ts +34 -0
- package/dist/schema-builders/mixed.d.ts.map +1 -0
- package/dist/schema-builders/mixed.js +8 -0
- package/dist/schema-builders/mixed.js.map +1 -0
- package/dist/schema-builders/number.d.ts +2 -2
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/number.js.map +1 -1
- package/dist/schema-builders/object-id.d.ts +2 -2
- package/dist/schema-builders/object-id.d.ts.map +1 -1
- package/dist/schema-builders/object-id.js.map +1 -1
- package/dist/schema-builders/ref.d.ts +2 -2
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/ref.js.map +1 -1
- package/dist/schema-builders/string.d.ts +35 -25
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.js.map +1 -1
- package/dist/schema-builders/uuid.d.ts +34 -0
- package/dist/schema-builders/uuid.d.ts.map +1 -0
- package/dist/schema-builders/uuid.js +8 -0
- package/dist/schema-builders/uuid.js.map +1 -0
- package/dist/types/aggregate-paginate.d.ts +24 -8
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +9 -4
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +6 -2
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +17 -12
- package/src/_internals.ts +3 -0
- package/src/builders.ts +18 -32
- package/src/connection.ts +14 -0
- package/src/options.ts +8 -1
- package/src/plugins/lean-decimal128-to-string.ts +149 -0
- package/src/plugins/normalize.ts +39 -15
- package/src/schema-builders/base.ts +6 -1
- package/src/schema-builders/bigint.ts +190 -0
- package/src/schema-builders/boolean.ts +1 -4
- package/src/schema-builders/date.ts +2 -2
- package/src/schema-builders/decimal128.ts +21 -14
- package/src/schema-builders/double.ts +198 -0
- package/src/schema-builders/index.ts +15 -0
- package/src/schema-builders/int32.ts +191 -0
- package/src/schema-builders/mixed.ts +37 -0
- package/src/schema-builders/number.ts +2 -2
- package/src/schema-builders/object-id.ts +2 -2
- package/src/schema-builders/ref.ts +2 -2
- package/src/schema-builders/string.ts +35 -25
- package/src/schema-builders/uuid.ts +48 -0
- package/src/types/aggregate-paginate.ts +29 -8
- package/src/types/index.ts +0 -5
- package/src/types/options.ts +12 -3
- package/src/types/paginate.ts +6 -2
- package/dist/constants.d.ts +0 -5
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js +0 -6
- package/dist/constants.js.map +0 -1
- package/dist/helpers.d.ts +0 -5
- package/dist/helpers.d.ts.map +0 -1
- package/dist/helpers.js +0 -9
- package/dist/helpers.js.map +0 -1
- package/src/constants.ts +0 -3
- package/src/helpers.ts +0 -5
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
IndexDirection,
|
|
4
|
+
IndexOptions,
|
|
5
|
+
Types,
|
|
6
|
+
} from 'mongoose';
|
|
7
|
+
import type { Merge } from 'type-fest';
|
|
8
|
+
|
|
9
|
+
import type { Readonlyable } from '../types/_internals';
|
|
10
|
+
|
|
11
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
|
+
|
|
13
|
+
type DoubleLimit = number | Types.Double;
|
|
14
|
+
type ExtendSchemaBuilder<
|
|
15
|
+
Props extends BaseProps,
|
|
16
|
+
ExtraOmitFields extends string,
|
|
17
|
+
> = Omit<
|
|
18
|
+
DoubleSchemaBuilder<Props, ExtraOmitFields>,
|
|
19
|
+
ExtraOmitFields | keyof Props
|
|
20
|
+
>;
|
|
21
|
+
|
|
22
|
+
interface BaseProps {
|
|
23
|
+
type: typeof Schema.Types.Double;
|
|
24
|
+
}
|
|
25
|
+
|
|
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
|
+
export interface DoubleSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
42
|
+
default: <
|
|
43
|
+
T extends ((this: any, doc: any) => D | null | undefined) | D | null | undefined,
|
|
44
|
+
D extends Types.Double,
|
|
45
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
46
|
+
Merge<Props, { default: T }>,
|
|
47
|
+
ExtraOmitFields
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
enum: <
|
|
51
|
+
T extends
|
|
52
|
+
| Readonlyable<(N | null)[]>
|
|
53
|
+
| { [path: string]: N | null }
|
|
54
|
+
| { message?: M; values: Readonlyable<(N | null)[]> },
|
|
55
|
+
M extends string,
|
|
56
|
+
N extends number | Types.Double,
|
|
57
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
58
|
+
Merge<Props, DoubleValidationSchema>,
|
|
59
|
+
'enum' | ExtraOmitFields
|
|
60
|
+
>;
|
|
61
|
+
|
|
62
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
63
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
64
|
+
Merge<Props, { index: T }>,
|
|
65
|
+
ExtraOmitFields
|
|
66
|
+
>;
|
|
67
|
+
|
|
68
|
+
max: <
|
|
69
|
+
T extends L | Readonlyable<[L, S]>,
|
|
70
|
+
L extends DoubleLimit,
|
|
71
|
+
S extends string,
|
|
72
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
73
|
+
Merge<Props, DoubleValidationSchema>,
|
|
74
|
+
'max' | ExtraOmitFields
|
|
75
|
+
>;
|
|
76
|
+
|
|
77
|
+
min: <
|
|
78
|
+
T extends L | Readonlyable<[L, S]>,
|
|
79
|
+
L extends DoubleLimit,
|
|
80
|
+
S extends string,
|
|
81
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
82
|
+
Merge<Props, DoubleValidationSchema>,
|
|
83
|
+
'min' | ExtraOmitFields
|
|
84
|
+
>;
|
|
85
|
+
|
|
86
|
+
nonRequired: Props;
|
|
87
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
88
|
+
required: Merge<Props, { required: true }>;
|
|
89
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
90
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
91
|
+
}
|
|
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
|
+
// Functions
|
|
103
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Double);
|
|
104
|
+
|
|
105
|
+
function createLimitValidator(
|
|
106
|
+
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
|
+
];
|
|
115
|
+
|
|
116
|
+
const numericLimit = typeof limit === 'number' ? limit : limit.valueOf();
|
|
117
|
+
if (!Number.isFinite(numericLimit)) throw new RangeError(`Double ${type} limit must be finite`);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
121
|
+
[type]: numericLimit,
|
|
122
|
+
type,
|
|
123
|
+
validator: (value) => {
|
|
124
|
+
if (value === null || value === undefined) return true;
|
|
125
|
+
|
|
126
|
+
return type === 'min' ? value.valueOf() >= numericLimit : value.valueOf() <= numericLimit;
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function doubleSchemaBuilder() {
|
|
132
|
+
const schema: Record<string, any> = {};
|
|
133
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
134
|
+
return new Proxy(
|
|
135
|
+
baseBuilder,
|
|
136
|
+
{
|
|
137
|
+
get(target, key, receiver) {
|
|
138
|
+
if (key === 'enum') {
|
|
139
|
+
if (schema.validate?.some(({ type }: { type: string }) => type === 'enum')) {
|
|
140
|
+
throw new Error('Duplicate schema attribute: enum');
|
|
141
|
+
}
|
|
142
|
+
|
|
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);
|
|
156
|
+
|
|
157
|
+
const allowedValues = new Set(
|
|
158
|
+
enumValues.map((value) => value === null ? null : value.valueOf()),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const validator: DoubleEnumValidator = {
|
|
162
|
+
enumValues,
|
|
163
|
+
message: message ?? defaultEnumValidateMessage,
|
|
164
|
+
type: 'enum',
|
|
165
|
+
validator: (value) => {
|
|
166
|
+
if (value === null || value === undefined) return true;
|
|
167
|
+
|
|
168
|
+
return allowedValues.has(value.valueOf());
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
schema.validate = [
|
|
173
|
+
...schema.validate ?? [],
|
|
174
|
+
validator,
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
return receiver;
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (key === 'max' || key === 'min') {
|
|
182
|
+
return (value: DoubleLimit | Readonlyable<[DoubleLimit, string]>) => {
|
|
183
|
+
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
|
+
];
|
|
189
|
+
|
|
190
|
+
return receiver;
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return Reflect.get(target, key, receiver);
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
) as DoubleSchemaBuilder;
|
|
198
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export { bigintSchemaBuilder as bigint } from './bigint';
|
|
2
|
+
export * from './bigint';
|
|
3
|
+
|
|
1
4
|
export { booleanSchemaBuilder as boolean } from './boolean';
|
|
2
5
|
export * from './boolean';
|
|
3
6
|
|
|
@@ -7,6 +10,15 @@ export * from './date';
|
|
|
7
10
|
export { decimal128SchemaBuilder as decimal128 } from './decimal128';
|
|
8
11
|
export * from './decimal128';
|
|
9
12
|
|
|
13
|
+
export { doubleSchemaBuilder as double } from './double';
|
|
14
|
+
export * from './double';
|
|
15
|
+
|
|
16
|
+
export { int32SchemaBuilder as int32 } from './int32';
|
|
17
|
+
export * from './int32';
|
|
18
|
+
|
|
19
|
+
export { mixedSchemaBuilder as mixed } from './mixed';
|
|
20
|
+
export * from './mixed';
|
|
21
|
+
|
|
10
22
|
export { numberSchemaBuilder as number } from './number';
|
|
11
23
|
export * from './number';
|
|
12
24
|
|
|
@@ -18,3 +30,6 @@ export * from './ref';
|
|
|
18
30
|
|
|
19
31
|
export { stringSchemaBuilder as string } from './string';
|
|
20
32
|
export * from './string';
|
|
33
|
+
|
|
34
|
+
export { uuidSchemaBuilder as uuid } from './uuid';
|
|
35
|
+
export * from './uuid';
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
DefaultType,
|
|
4
|
+
IndexDirection,
|
|
5
|
+
IndexOptions,
|
|
6
|
+
} from 'mongoose';
|
|
7
|
+
import type { Merge } from 'type-fest';
|
|
8
|
+
|
|
9
|
+
import type { Readonlyable } from '../types/_internals';
|
|
10
|
+
|
|
11
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
12
|
+
|
|
13
|
+
type ExtendSchemaBuilder<
|
|
14
|
+
Props extends BaseProps,
|
|
15
|
+
ExtraOmitFields extends string,
|
|
16
|
+
> = Omit<
|
|
17
|
+
Int32SchemaBuilder<Props, ExtraOmitFields>,
|
|
18
|
+
ExtraOmitFields | keyof Props
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
type Int32Limit = number;
|
|
22
|
+
|
|
23
|
+
interface BaseProps {
|
|
24
|
+
type: typeof Schema.Types.Int32;
|
|
25
|
+
}
|
|
26
|
+
|
|
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
|
+
export interface Int32SchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
43
|
+
default: <
|
|
44
|
+
T extends ((this: any, doc: any) => DefaultType<D> | null | undefined) | DefaultType<D> | null | undefined,
|
|
45
|
+
D extends number,
|
|
46
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
47
|
+
Merge<Props, { default: T }>,
|
|
48
|
+
ExtraOmitFields
|
|
49
|
+
>;
|
|
50
|
+
|
|
51
|
+
enum: <
|
|
52
|
+
T extends
|
|
53
|
+
| Readonlyable<(N | null)[]>
|
|
54
|
+
| { [path: string]: N | null }
|
|
55
|
+
| { message?: M; values: Readonlyable<(N | null)[]> },
|
|
56
|
+
M extends string,
|
|
57
|
+
N extends number,
|
|
58
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
59
|
+
Merge<Props, Int32ValidationSchema>,
|
|
60
|
+
'enum' | ExtraOmitFields
|
|
61
|
+
>;
|
|
62
|
+
|
|
63
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
64
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
65
|
+
Merge<Props, { index: T }>,
|
|
66
|
+
ExtraOmitFields
|
|
67
|
+
>;
|
|
68
|
+
|
|
69
|
+
max: <
|
|
70
|
+
T extends L | Readonlyable<[L, S]>,
|
|
71
|
+
L extends Int32Limit,
|
|
72
|
+
S extends string,
|
|
73
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
74
|
+
Merge<Props, Int32ValidationSchema>,
|
|
75
|
+
'max' | ExtraOmitFields
|
|
76
|
+
>;
|
|
77
|
+
|
|
78
|
+
min: <
|
|
79
|
+
T extends L | Readonlyable<[L, S]>,
|
|
80
|
+
L extends Int32Limit,
|
|
81
|
+
S extends string,
|
|
82
|
+
>(value: T) => ExtendSchemaBuilder<
|
|
83
|
+
Merge<Props, Int32ValidationSchema>,
|
|
84
|
+
'min' | ExtraOmitFields
|
|
85
|
+
>;
|
|
86
|
+
|
|
87
|
+
nonRequired: Props;
|
|
88
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
89
|
+
required: Merge<Props, { required: true }>;
|
|
90
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
91
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
92
|
+
}
|
|
93
|
+
|
|
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
|
+
// Functions
|
|
104
|
+
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Int32);
|
|
105
|
+
|
|
106
|
+
function createLimitValidator(
|
|
107
|
+
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
|
+
];
|
|
116
|
+
|
|
117
|
+
if (!Number.isFinite(numericLimit)) throw new RangeError(`Int32 ${type} limit must be finite`);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
message: message ?? (type === 'min' ? defaultMinValidateMessage : defaultMaxValidateMessage),
|
|
121
|
+
[type]: numericLimit,
|
|
122
|
+
type,
|
|
123
|
+
validator: (value) => {
|
|
124
|
+
if (value === null || value === undefined) return true;
|
|
125
|
+
|
|
126
|
+
return type === 'min' ? value >= numericLimit : value <= numericLimit;
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function int32SchemaBuilder() {
|
|
132
|
+
const schema: Record<string, any> = {};
|
|
133
|
+
const baseBuilder = baseBuilderFactory(schema);
|
|
134
|
+
return new Proxy(
|
|
135
|
+
baseBuilder,
|
|
136
|
+
{
|
|
137
|
+
get(target, key, receiver) {
|
|
138
|
+
if (key === 'enum') {
|
|
139
|
+
if (schema.validate?.some(({ type }: { type: string }) => type === 'enum')) {
|
|
140
|
+
throw new Error('Duplicate schema attribute: enum');
|
|
141
|
+
}
|
|
142
|
+
|
|
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);
|
|
156
|
+
|
|
157
|
+
const allowedValues = new Set(enumValues);
|
|
158
|
+
const validator: Int32EnumValidator = {
|
|
159
|
+
enumValues,
|
|
160
|
+
message: message ?? defaultEnumValidateMessage,
|
|
161
|
+
type: 'enum',
|
|
162
|
+
validator: (value) => value === null || value === undefined || allowedValues.has(value),
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
schema.validate = [
|
|
166
|
+
...schema.validate ?? [],
|
|
167
|
+
validator,
|
|
168
|
+
];
|
|
169
|
+
|
|
170
|
+
return receiver;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (key === 'max' || key === 'min') {
|
|
175
|
+
return (value: Int32Limit | Readonlyable<[Int32Limit, string]>) => {
|
|
176
|
+
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
|
+
];
|
|
182
|
+
|
|
183
|
+
return receiver;
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return Reflect.get(target, key, receiver);
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
) as Int32SchemaBuilder;
|
|
191
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
IndexDirection,
|
|
4
|
+
IndexOptions,
|
|
5
|
+
} from 'mongoose';
|
|
6
|
+
import type { Merge } from 'type-fest';
|
|
7
|
+
|
|
8
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
9
|
+
|
|
10
|
+
type ExtendSchemaBuilder<
|
|
11
|
+
Props extends BaseProps,
|
|
12
|
+
ExtraOmitFields extends string,
|
|
13
|
+
> = Omit<
|
|
14
|
+
MixedSchemaBuilder<Props, ExtraOmitFields>,
|
|
15
|
+
ExtraOmitFields | keyof Props
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
interface BaseProps {
|
|
19
|
+
type: typeof Schema.Types.Mixed;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MixedSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
23
|
+
default: <T>(value: T) => ExtendSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
|
|
24
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
25
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
26
|
+
Merge<Props, { index: T }>,
|
|
27
|
+
ExtraOmitFields
|
|
28
|
+
>;
|
|
29
|
+
|
|
30
|
+
nonRequired: Props;
|
|
31
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
32
|
+
required: Merge<Props, { required: true }>;
|
|
33
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
34
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const mixedSchemaBuilder = createBaseSchemaBuilderFactory<MixedSchemaBuilder>(Schema.Types.Mixed);
|
|
@@ -36,9 +36,9 @@ export interface NumberSchemaBuilder<
|
|
|
36
36
|
|
|
37
37
|
enum: <
|
|
38
38
|
T extends
|
|
39
|
-
| Readonlyable<
|
|
39
|
+
| Readonlyable<(N | null)[]>
|
|
40
40
|
| { [path: string]: N | null }
|
|
41
|
-
| { message?: M; values: Readonlyable<
|
|
41
|
+
| { message?: M; values: Readonlyable<(N | null)[]> },
|
|
42
42
|
M extends string,
|
|
43
43
|
N extends number,
|
|
44
44
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -37,9 +37,9 @@ export interface ObjectIdSchemaBuilder<
|
|
|
37
37
|
|
|
38
38
|
enum: <
|
|
39
39
|
T extends
|
|
40
|
-
| Readonlyable<
|
|
40
|
+
| Readonlyable<(null | O)[]>
|
|
41
41
|
| { [path: string]: null | O }
|
|
42
|
-
| { message?: M; values: Readonlyable<
|
|
42
|
+
| { message?: M; values: Readonlyable<(null | O)[]> },
|
|
43
43
|
M extends string,
|
|
44
44
|
O extends Types.ObjectId,
|
|
45
45
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -38,9 +38,9 @@ export interface RefSchemaBuilder<
|
|
|
38
38
|
|
|
39
39
|
enum: <
|
|
40
40
|
T extends
|
|
41
|
-
| Readonlyable<
|
|
41
|
+
| Readonlyable<(null | O)[]>
|
|
42
42
|
| { [path: string]: null | O }
|
|
43
|
-
| { message?: M; values: Readonlyable<
|
|
43
|
+
| { message?: M; values: Readonlyable<(null | O)[]> },
|
|
44
44
|
M extends string,
|
|
45
45
|
O extends Types.ObjectId,
|
|
46
46
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -46,9 +46,9 @@ export interface StringSchemaBuilder<
|
|
|
46
46
|
|
|
47
47
|
enum: <
|
|
48
48
|
T extends
|
|
49
|
-
| Readonlyable<
|
|
49
|
+
| Readonlyable<(null | S)[]>
|
|
50
50
|
| { [path: string]: null | S }
|
|
51
|
-
| { message?: M; values: Readonlyable<
|
|
51
|
+
| { message?: M; values: Readonlyable<(null | S)[]> },
|
|
52
52
|
M extends string,
|
|
53
53
|
S extends string,
|
|
54
54
|
>(value: T) => ExtendSchemaBuilder<
|
|
@@ -63,13 +63,14 @@ export interface StringSchemaBuilder<
|
|
|
63
63
|
>;
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
|
-
* Adds IPv4 validation
|
|
67
|
-
* Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
|
|
66
|
+
* Adds IPv4 address validation and enables input trimming.
|
|
68
67
|
*
|
|
69
|
-
* @
|
|
70
|
-
*
|
|
68
|
+
* @remarks
|
|
69
|
+
* Replaces the configured custom validator and sets `trim` to `true` on this builder.
|
|
71
70
|
*
|
|
72
|
-
* @
|
|
71
|
+
* @param message - The validation error message. Defaults to the built-in IPv4 validation message.
|
|
72
|
+
*
|
|
73
|
+
* @returns This instance for chaining.
|
|
73
74
|
*/
|
|
74
75
|
ipv4: <T extends string = typeof defaultIpv4ValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
75
76
|
Merge<Props, IpSchema<T>>,
|
|
@@ -77,13 +78,14 @@ export interface StringSchemaBuilder<
|
|
|
77
78
|
>;
|
|
78
79
|
|
|
79
80
|
/**
|
|
80
|
-
* Adds IPv6 validation
|
|
81
|
-
*
|
|
81
|
+
* Adds IPv6 address validation and enables input trimming.
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* Replaces the configured custom validator and sets `trim` to `true` on this builder.
|
|
82
85
|
*
|
|
83
|
-
* @param message - The
|
|
84
|
-
* IPv6 validation message.
|
|
86
|
+
* @param message - The validation error message. Defaults to the built-in IPv6 validation message.
|
|
85
87
|
*
|
|
86
|
-
* @returns
|
|
88
|
+
* @returns This instance for chaining.
|
|
87
89
|
*/
|
|
88
90
|
ipv6: <T extends string = typeof defaultIpv6ValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
89
91
|
Merge<Props, IpSchema<T>>,
|
|
@@ -91,12 +93,15 @@ export interface StringSchemaBuilder<
|
|
|
91
93
|
>;
|
|
92
94
|
|
|
93
95
|
/**
|
|
94
|
-
* Sets
|
|
95
|
-
*
|
|
96
|
+
* Sets equal minimum and maximum string lengths.
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* Assigns the same value to `minlength` and `maxlength`, replacing any previously configured limits.
|
|
96
100
|
*
|
|
97
|
-
* @param value -
|
|
101
|
+
* @param value - The exact length in UTF-16 code units, or a tuple containing the length and a validation
|
|
102
|
+
* error message.
|
|
98
103
|
*
|
|
99
|
-
* @returns
|
|
104
|
+
* @returns This instance for chaining.
|
|
100
105
|
*/
|
|
101
106
|
length: <
|
|
102
107
|
T extends L | Readonlyable<[L, S]>,
|
|
@@ -110,13 +115,15 @@ export interface StringSchemaBuilder<
|
|
|
110
115
|
lowercase: ExtendSchemaBuilder<Merge<Props, { lowercase: true }>, ExtraOmitFields>;
|
|
111
116
|
|
|
112
117
|
/**
|
|
113
|
-
*
|
|
114
|
-
* The validation message can be customized.
|
|
118
|
+
* Configures regular expression validation for the string field.
|
|
115
119
|
*
|
|
116
|
-
* @
|
|
117
|
-
*
|
|
120
|
+
* @remarks
|
|
121
|
+
* Replaces any previously configured `match` option.
|
|
118
122
|
*
|
|
119
|
-
* @
|
|
123
|
+
* @param regex - The regular expression used by Mongoose to validate the string.
|
|
124
|
+
* @param message - The validation error message. If omitted, Mongoose uses its default message.
|
|
125
|
+
*
|
|
126
|
+
* @returns This instance for chaining.
|
|
120
127
|
*/
|
|
121
128
|
match: <
|
|
122
129
|
T extends RegExp,
|
|
@@ -154,12 +161,15 @@ export interface StringSchemaBuilder<
|
|
|
154
161
|
uppercase: ExtendSchemaBuilder<Merge<Props, { uppercase: true }>, ExtraOmitFields>;
|
|
155
162
|
|
|
156
163
|
/**
|
|
157
|
-
* Adds WHATWG URL validation
|
|
158
|
-
*
|
|
164
|
+
* Adds WHATWG URL parseability validation.
|
|
165
|
+
*
|
|
166
|
+
* @remarks
|
|
167
|
+
* Accepts URLs with credentials and non-HTTP schemes. Performs no DNS lookup or stored-value normalization,
|
|
168
|
+
* and does not enable trimming. Replaces the configured custom validator on this builder.
|
|
159
169
|
*
|
|
160
|
-
* @param message -
|
|
170
|
+
* @param message - The validation error message. Defaults to the built-in URL validation message.
|
|
161
171
|
*
|
|
162
|
-
* @returns
|
|
172
|
+
* @returns This instance for chaining.
|
|
163
173
|
*/
|
|
164
174
|
url: <T extends string = typeof defaultUrlValidateMessage>(message?: T) => ExtendSchemaBuilder<
|
|
165
175
|
Merge<Props, UrlSchema<T>>,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Schema } from 'mongoose';
|
|
2
|
+
import type {
|
|
3
|
+
IndexDirection,
|
|
4
|
+
IndexOptions,
|
|
5
|
+
Types,
|
|
6
|
+
} from 'mongoose';
|
|
7
|
+
import type { Merge } from 'type-fest';
|
|
8
|
+
|
|
9
|
+
import { createBaseSchemaBuilderFactory } from './base';
|
|
10
|
+
|
|
11
|
+
type ExtendSchemaBuilder<
|
|
12
|
+
Props extends BaseProps,
|
|
13
|
+
ExtraOmitFields extends string,
|
|
14
|
+
> = Omit<
|
|
15
|
+
UUIDSchemaBuilder<Props, ExtraOmitFields>,
|
|
16
|
+
ExtraOmitFields | keyof Props
|
|
17
|
+
>;
|
|
18
|
+
|
|
19
|
+
interface BaseProps {
|
|
20
|
+
type: typeof Schema.Types.UUID;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface UUIDSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
24
|
+
default: <
|
|
25
|
+
T extends
|
|
26
|
+
| ((this: any, doc: any) => null | string | Types.UUID | undefined)
|
|
27
|
+
| null
|
|
28
|
+
| string
|
|
29
|
+
| Types.UUID
|
|
30
|
+
| undefined,
|
|
31
|
+
>(
|
|
32
|
+
value: T,
|
|
33
|
+
) => ExtendSchemaBuilder<Merge<Props, { default: T }>, ExtraOmitFields>;
|
|
34
|
+
|
|
35
|
+
immutable: ExtendSchemaBuilder<Merge<Props, { immutable: true }>, ExtraOmitFields>;
|
|
36
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendSchemaBuilder<
|
|
37
|
+
Merge<Props, { index: T }>,
|
|
38
|
+
ExtraOmitFields
|
|
39
|
+
>;
|
|
40
|
+
|
|
41
|
+
nonRequired: Props;
|
|
42
|
+
private: ExtendSchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;
|
|
43
|
+
required: Merge<Props, { required: true }>;
|
|
44
|
+
sparse: ExtendSchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;
|
|
45
|
+
unique: ExtendSchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const uuidSchemaBuilder = createBaseSchemaBuilderFactory<UUIDSchemaBuilder>(Schema.Types.UUID);
|