Skip to content

Commit d3ce5b0

Browse files
fix: add sanity checks to type immutablity override settings
1 parent b42c6ab commit d3ce5b0

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

src/settings/immutability.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,47 @@ function loadImmutabilityOverrides(
7171
? overridesSetting
7272
: overridesSetting.values ?? [];
7373

74-
const upgraded = raw.map(
75-
({ type, to, from }) =>
76-
({
77-
type,
78-
to: typeof to === "string" ? Immutability[to] : to,
79-
from:
80-
from === undefined
81-
? undefined
82-
: typeof from === "string"
83-
? Immutability[from]
84-
: from,
85-
}) as ImmutabilityOverrides[number],
86-
);
74+
const upgraded = raw.map((rawValue) => {
75+
const { type, to, from, ...rest } = rawValue;
76+
const value = {
77+
type,
78+
to: typeof to === "string" ? Immutability[to] : to,
79+
from:
80+
from === undefined
81+
? undefined
82+
: typeof from === "string"
83+
? Immutability[from]
84+
: from,
85+
} as ImmutabilityOverrides[number];
86+
87+
if (value.type === undefined) {
88+
// eslint-disable-next-line functional/no-throw-statements
89+
throw new Error(
90+
`Override is missing required "type" property. Value: "${JSON.stringify(
91+
rawValue,
92+
)}"`,
93+
);
94+
}
95+
if (value.to === undefined) {
96+
// eslint-disable-next-line functional/no-throw-statements
97+
throw new Error(
98+
`Override is missing required "to" property. Value: "${JSON.stringify(
99+
rawValue,
100+
)}"`,
101+
);
102+
}
103+
const restKeys = Object.keys(rest);
104+
if (restKeys.length > 0) {
105+
// eslint-disable-next-line functional/no-throw-statements
106+
throw new Error(
107+
`Override is contains unknown property(s) "${restKeys.join(
108+
", ",
109+
)}". Value: "${JSON.stringify(rawValue)}"`,
110+
);
111+
}
112+
113+
return value;
114+
});
87115

88116
const keepDefault =
89117
Array.isArray(overridesSetting) || overridesSetting.keepDefault !== false;

0 commit comments

Comments
 (0)