-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
processProp.ts
519 lines (437 loc) · 17.1 KB
/
processProp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import { logger } from '../logSettings';
import { buildSchema, mongoose } from '../typegoose';
import {
AnyParamConstructor,
DecoratedPropertyMetadata,
DiscriminatorObject,
KeyStringAny,
NestedDiscriminatorsMap,
VirtualPopulateMap,
} from '../types';
import { DecoratorKeys, WhatIsIt } from './constants';
import { schemas } from './data';
import { InvalidTypeError, NotAllVPOPElementsError, NotNumberTypeError, NotStringTypeError } from './errors';
import * as utils from './utils';
/**
* Function that is the actual processing of the prop's (used for caching)
* @param input All the options needed for prop's
*/
export function processProp(input: DecoratedPropertyMetadata): void {
const { key, target } = input;
const name = utils.getName(target);
const rawOptions: KeyStringAny = Object.assign({}, input.options);
let Type: any | undefined = Reflect.getMetadata(DecoratorKeys.Type, target, key);
const propKind = input.whatis ?? detectWhatIsIt(Type);
logger.debug('Starting to process "%s.%s"', name, key);
utils.assertion(typeof key === 'string', new Error(`Property Key in typegoose cannot be an symbol! (${name}.${String(key)})`));
optionDeprecation(rawOptions);
{
// soft errors & "type"-alias mapping
switch (propKind) {
case WhatIsIt.NONE:
if ('items' in rawOptions) {
logger.warn('You might not want to use option "items" for an non-array @prop type (%s.%s)', name, key);
}
if ('of' in rawOptions) {
logger.warn('You might not want to use option "of" for an non-map @prop type (%s.%s)', name, key);
}
break;
case WhatIsIt.ARRAY:
if ('items' in rawOptions) {
rawOptions.type = rawOptions.items;
delete rawOptions.items;
}
if ('of' in rawOptions) {
logger.warn('You might not want to use option "of" where the "design:type" is "Array" (%s.%s)', name, key);
}
// set the "Type" to undefined, if "ref" or "refPath" are defined, otherwise the "refType" will be wrong
if (('ref' in rawOptions || 'refPath' in rawOptions) && !('type' in rawOptions)) {
Type = undefined;
}
break;
case WhatIsIt.MAP:
if ('of' in rawOptions) {
rawOptions.type = rawOptions.of;
delete rawOptions.of;
}
if ('items' in rawOptions) {
logger.warn('You might not want to use option "items" where the "design:type" is "Map" (%s.%s)', name, key);
}
break;
}
}
if (!utils.isNullOrUndefined(rawOptions.type)) {
logger.info('Prop Option "type" is set to ', rawOptions.type);
const gotType = utils.getType(rawOptions.type);
Type = gotType.type;
if (gotType.dim > 0) {
rawOptions.dim = gotType.dim;
}
delete rawOptions.type;
}
// prevent "infinite" buildSchema loop / Maximum Stack size exceeded
if (Type === target.constructor) {
throw new TypeError(
'It seems like the type used is the same as the target class, which is not supported\n' +
`Please look at https://github.com/typegoose/typegoose/issues/42 for more information [E004]`
);
}
// map to correct buffer type, otherwise it would result in "Mixed"
if (Type === mongoose.Types.Buffer) {
Type = mongoose.Schema.Types.Buffer;
}
// confirm that "WhatIsIt" is an ARRAY and that the Type is still an *ARRAY and set them to Mixed
// for issues like https://github.com/typegoose/typegoose/issues/300
if (propKind === WhatIsIt.ARRAY && detectWhatIsIt(Type) === WhatIsIt.ARRAY) {
logger.debug('Type is still *ARRAY, defaulting to Mixed');
Type = mongoose.Schema.Types.Mixed;
}
if (utils.isNotDefined(Type)) {
buildSchema(Type);
}
if ('discriminators' in rawOptions) {
logger.debug('Found option "discriminators" in "%s.%s"', name, key);
const gotType = utils.getType(rawOptions.discriminators, true);
utils.assertion(
gotType.dim === 1,
new Error(
`"PropOptions.discriminators" dosnt support Arrays higher and lower than 1 (got "${gotType.dim}" dimensions at "${name}.${key}") [E020]`
)
);
const discriminators: DiscriminatorObject[] = (gotType.type as (AnyParamConstructor<any> | DiscriminatorObject)[]).map((val, index) => {
if (utils.isConstructor(val)) {
return { type: val };
}
if (typeof val === 'object') {
if (!('type' in val)) {
throw new Error(`"${name}.${key}" discriminator index "${index}" is an object, but does not contain the "type" property!`);
}
return val;
}
throw new Error(`"${name}.${key}" discriminators index "${index}" is not an object or an constructor!`);
});
const disMap: NestedDiscriminatorsMap = new Map(Reflect.getMetadata(DecoratorKeys.NestedDiscriminators, target.constructor) ?? []);
disMap.set(key, discriminators);
Reflect.defineMetadata(DecoratorKeys.NestedDiscriminators, disMap, target.constructor);
delete rawOptions.discriminators;
}
// allow setting the type asynchronously
if ('ref' in rawOptions) {
const gotType = utils.getType(rawOptions.ref);
utils.assertion(
gotType.dim === 0,
new Error(`"PropOptions.ref" dosnt support Arrays (got "${gotType.dim}" dimensions at "${name}.${key}") [E021]`)
);
rawOptions.ref = gotType.type;
utils.assertion(!utils.isNullOrUndefined(rawOptions.ref), new Error(`Option "ref" for "${name}.${key}" is null/undefined! [E005]`));
rawOptions.ref =
typeof rawOptions.ref === 'string'
? rawOptions.ref
: utils.isConstructor(rawOptions.ref)
? utils.getName(rawOptions.ref)
: rawOptions.ref;
}
if (utils.isWithVirtualPOP(rawOptions)) {
if (!utils.includesAllVirtualPOP(rawOptions)) {
throw new NotAllVPOPElementsError(name, key);
}
const virtuals: VirtualPopulateMap = new Map(Reflect.getMetadata(DecoratorKeys.VirtualPopulate, target.constructor) ?? []);
virtuals.set(key, rawOptions);
Reflect.defineMetadata(DecoratorKeys.VirtualPopulate, virtuals, target.constructor);
return;
}
if ('justOne' in rawOptions) {
logger.warn(
`Option "justOne" is defined in "${name}.${key}" but no Virtual-Populate-Options!\n` +
'Look here for more: https://typegoose.github.io/typegoose/docs/api/virtuals#virtual-populate'
);
}
const schemaProp = utils.initProperty(name, key, propKind);
if (!utils.isNullOrUndefined(rawOptions.set) || !utils.isNullOrUndefined(rawOptions.get)) {
utils.assertion(typeof rawOptions.set === 'function', new TypeError(`"${name}.${key}" does not have a set function! [E007]`));
utils.assertion(typeof rawOptions.get === 'function', new TypeError(`"${name}.${key}" does not have a get function! [E007]`));
// use an compiled Schema if the type is an Nested Class
const useType = schemas.has(utils.getName(Type)) ? buildSchema(Type) : Type;
switch (propKind) {
case WhatIsIt.ARRAY:
schemaProp[key] = {
...schemaProp[key][0],
...utils.mapArrayOptions(rawOptions, useType, target, key),
};
return;
case WhatIsIt.MAP:
const mapped = utils.mapOptions(rawOptions, useType, target, key);
schemaProp[key] = {
...schemaProp[key],
...mapped.outer,
type: Map,
of: { type: useType, ...mapped.inner },
};
return;
case WhatIsIt.NONE:
schemaProp[key] = {
...schemaProp[key],
...rawOptions,
type: useType,
};
return;
default:
/* istanbul ignore next */ // ignore because this case should really never happen (typescript prevents this)
throw new Error(`"${propKind}"(whatis(primitive)) is invalid for "${name}.${key}" [E013]`);
}
}
// use "Type" if it is an suitable ref-type, otherwise default back to "ObjectId"
const refType = utils.isAnRefType(Type) ? Type : mongoose.Schema.Types.ObjectId;
if ('ref' in rawOptions) {
const ref = rawOptions.ref;
delete rawOptions.ref;
switch (propKind) {
case WhatIsIt.ARRAY:
schemaProp[key] = utils.createArrayFromDimensions(
rawOptions,
{
...schemaProp[key][0],
type: refType,
ref,
...rawOptions,
},
name,
key
);
break;
case WhatIsIt.NONE:
schemaProp[key] = {
...schemaProp[key],
type: refType,
ref,
...rawOptions,
};
break;
default:
throw new TypeError(`"ref" is not supported for "${propKind}"! (${name}, ${key}) [E023]`);
}
return;
}
const refPath = rawOptions.refPath;
if (refPath) {
utils.assertion(typeof refPath === 'string', new TypeError(`"refPath" for "${name}, ${key}" should be of type String! [E008]`));
delete rawOptions.refPath;
switch (propKind) {
case WhatIsIt.ARRAY:
schemaProp[key] = utils.createArrayFromDimensions(
rawOptions,
{
...schemaProp[key][0],
type: refType,
refPath,
...rawOptions,
},
name,
key
);
break;
case WhatIsIt.NONE:
schemaProp[key] = {
...schemaProp[key],
type: refType,
refPath,
...rawOptions,
};
break;
default:
throw new TypeError(`"refPath" is not supported for "${propKind}"! (${name}, ${key}) [E023]`);
}
return;
}
// check if Type is actually a real working Type
if (utils.isNullOrUndefined(Type) || typeof Type !== 'function') {
throw new InvalidTypeError(name, key, Type);
}
const enumOption = rawOptions.enum;
if (!utils.isNullOrUndefined(enumOption)) {
// check if the supplied value is already "mongoose-consumeable"
if (!Array.isArray(enumOption)) {
if (Type === String || Type === mongoose.Schema.Types.String) {
rawOptions.enum = Object.entries<string>(enumOption) // get all key-value pairs of the enum
// no reverse-filtering because if it is full of strings, there is no reverse mapping
.map(([enumKey, enumValue]) => {
// convert key-value pairs to an mongoose-usable enum
// safeguard, this should never happen because TypeScript only sets "design:type" to "String"
// if the enum is full of strings
if (typeof enumValue !== 'string') {
throw new NotStringTypeError(name, key, enumKey, typeof enumValue);
}
return enumValue;
});
} else if (Type === Number || Type === mongoose.Schema.Types.Number) {
rawOptions.enum = Object.entries<string | number>(enumOption) // get all key-value pairs of the enum
// filter out the "reverse (value -> name) mappings"
// https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
.filter(([enumKey, enumValue], _i, arr) => {
// safeguard, this should never happen because typescript only sets "design:type" to "Number"
// if the enum is full of numbers
if (utils.isNullOrUndefined(enumValue) || arr.findIndex(([k]) => k === enumValue.toString()) <= -1) {
// if there is no reverse mapping, throw an error
throw new NotNumberTypeError(name, key, enumKey, typeof enumValue);
}
return typeof enumValue === 'number';
})
.map(([enumKey, enumValue]) => {
// convert key-value pairs to an mongoose-useable enum
if (typeof enumValue !== 'number') {
throw new NotNumberTypeError(name, key, enumKey, typeof enumValue);
}
return enumValue;
});
} else {
// this will happen if the enum type is not "String" or "Number"
// most likely this error happened because the code got transpiled with babel or "tsc --transpile-only"
throw new Error(
`Invalid type used for enums!, got: "${Type}" (${name}.${key}) [E012]` +
"Is the code transpiled with Babel or 'tsc --transpile-only' or 'ts-node --transpile-only'?\n" +
'See https://typegoose.github.io/typegoose/docs/api/decorators/prop/#enum'
);
}
}
}
if (!utils.isNullOrUndefined(rawOptions.addNullToEnum)) {
rawOptions.enum = Array.isArray(rawOptions.enum) ? rawOptions.enum : [];
rawOptions.enum.push(null);
delete rawOptions.addNullToEnum;
}
{
let included: string[] = utils.isWithStringValidate(rawOptions);
if (!utils.isString(Type)) {
// warn if String-Validate options are included, but is not string
utils.warnNotCorrectTypeOptions(name, key, 'String', 'String-Validate', included);
}
included = utils.isWithStringTransform(rawOptions);
if (!utils.isString(Type)) {
// warn if String-Transform options are included, but is not string
utils.warnNotCorrectTypeOptions(name, key, 'String', 'String-Transform', included);
}
included = utils.isWithNumberValidate(rawOptions);
if (!utils.isNumber(Type)) {
// warn if Number-Validate options are included, but is not number
utils.warnNotCorrectTypeOptions(name, key, 'Number', 'Number-Validate', included);
}
included = utils.isWithEnumValidate(rawOptions);
if (!utils.isString(Type) && !utils.isNumber(Type)) {
// warn if "enum" is included, but is not Number or String
utils.warnNotCorrectTypeOptions(name, key, 'String | Number', 'extra', included);
}
}
/** Is this Type (/Class) in the schemas Map? */
const isInSchemas = schemas.has(utils.getName(Type));
if (utils.isPrimitive(Type)) {
if (utils.isObject(Type, true)) {
utils.warnMixed(target, key);
}
switch (propKind) {
case WhatIsIt.ARRAY:
schemaProp[key] = {
...schemaProp[key][0],
...utils.mapArrayOptions(rawOptions, Type, target, key),
};
return;
case WhatIsIt.MAP:
const mapped = utils.mapOptions(rawOptions, Type, target, key);
schemaProp[key] = {
...schemaProp[key],
...mapped.outer,
type: Map,
of: { type: Type, ...mapped.inner },
};
return;
case WhatIsIt.NONE:
schemaProp[key] = {
...schemaProp[key],
...rawOptions,
type: Type,
};
return;
default:
/* istanbul ignore next */ // ignore because this case should really never happen (typescript prevents this)
throw new Error(`"${propKind}"(whatis(primitive)) is invalid for "${name}.${key}" [E013]`);
}
}
// If the 'Type' is not a 'Primitive Type' and no subschema was found treat the type as 'Object'
// so that mongoose can store it as nested document
if (utils.isObject(Type) && !isInSchemas) {
utils.warnMixed(target, key);
logger.warn(
'if someone can see this message, please open an new issue at https://github.com/typegoose/typegoose/issues with reproduction code for tests'
);
schemaProp[key] = {
...schemaProp[key],
...rawOptions,
type: mongoose.Schema.Types.Mixed,
};
return;
}
const virtualSchema = buildSchema(Type);
switch (propKind) {
case WhatIsIt.ARRAY:
schemaProp[key] = {
...schemaProp[key][0], // [0] is needed, because "initasArray" adds this (empty)
...utils.mapArrayOptions(rawOptions, virtualSchema, target, key, Type),
};
return;
case WhatIsIt.MAP:
const mapped = utils.mapOptions(rawOptions, virtualSchema, target, key, Type);
schemaProp[key] = {
...schemaProp[key],
...mapped.outer,
type: Map,
of: { type: virtualSchema, ...mapped.inner },
};
return;
case WhatIsIt.NONE:
schemaProp[key] = {
...schemaProp[key],
...rawOptions,
type: virtualSchema,
};
return;
default:
/* istanbul ignore next */ // ignore because this case should really never happen (typescript prevents this)
throw new Error(`"${propKind}"(whatis(subSchema)) is invalid for "${name}.${key}" [E013]`);
}
}
/**
* Check for deprecated options, and if needed process them
* @param options
*/
function optionDeprecation(options: any) {
if ('refType' in options) {
options.type = options.refType;
delete options.refType;
utils.deprecate(() => undefined, 'Option "refType" is deprecated, use option "type"', 'TDEP0003')();
}
if ('of' in options) {
utils.deprecate(() => undefined, 'Option "of" is deprecated, use option "type"', 'TDEP0003')();
}
if ('items' in options) {
utils.deprecate(() => undefined, 'Option "items" is deprecated, use option "type"', 'TDEP0003')();
}
}
/**
* Detect "WhatIsIt" based on "Type"
* @param Type The Type used for detection
*/
function detectWhatIsIt(Type: any): WhatIsIt {
logger.debug('Detecting WhatIsIt');
if (
Type === Array ||
Type === mongoose.Types.Array ||
Type === mongoose.Schema.Types.Array ||
Type === mongoose.Types.DocumentArray ||
Type === mongoose.Schema.Types.DocumentArray
) {
return WhatIsIt.ARRAY;
}
if (Type === Map || Type === mongoose.Types.Map || Type === mongoose.Schema.Types.Map) {
return WhatIsIt.MAP;
}
return WhatIsIt.NONE;
}