MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / processDecoratorParameters

Function processDecoratorParameters

packages/decorators/src/utils.ts:67–89  ·  view source on GitHub ↗
(params: Dictionary)

Source from the content-addressed store, hash-verified

65 * @internal
66 */
67export function processDecoratorParameters<T>(params: Dictionary): T {
68 const keys = Object.keys(params);
69 const values = Object.values(params);
70
71 if (!Utils.isPlainObject(values[0])) {
72 const lastKey = keys[keys.length - 1];
73 const last = params[lastKey];
74 delete params[lastKey];
75
76 return { ...last, ...params };
77 }
78
79 // validate only first parameter is used if its an option object
80 const empty = (v: unknown) => v == null || (Utils.isPlainObject(v) && !Utils.hasObjectKeys(v));
81 if (values.slice(1).some(v => !empty(v))) {
82 throw new Error(
83 'Mixing first decorator parameter as options object with other parameters is forbidden. ' +
84 'If you want to use the options parameter at first position, provide all options inside it.',
85 );
86 }
87
88 return values[0] as T;
89}
90
91/**
92 * Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`

Callers 8

ManyToManyFunction · 0.85
OneToManyFunction · 0.85
ManyToOneFunction · 0.85
OneToOneFunction · 0.85
ManyToManyFunction · 0.85
OneToManyFunction · 0.85
ManyToOneFunction · 0.85
OneToOneFunction · 0.85

Calls 5

emptyFunction · 0.85
isPlainObjectMethod · 0.80
sliceMethod · 0.80
keysMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected