Skip to content

Commit

Permalink
fix: add sanity checks to type immutablity override settings
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Jul 17, 2023
1 parent b42c6ab commit d3ce5b0
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/settings/immutability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,47 @@ function loadImmutabilityOverrides(
? overridesSetting
: overridesSetting.values ?? [];

const upgraded = raw.map(
({ type, to, from }) =>
({
type,
to: typeof to === "string" ? Immutability[to] : to,
from:
from === undefined
? undefined
: typeof from === "string"
? Immutability[from]
: from,
}) as ImmutabilityOverrides[number],
);
const upgraded = raw.map((rawValue) => {
const { type, to, from, ...rest } = rawValue;
const value = {
type,
to: typeof to === "string" ? Immutability[to] : to,
from:
from === undefined
? undefined
: typeof from === "string"
? Immutability[from]
: from,
} as ImmutabilityOverrides[number];

if (value.type === undefined) {
// eslint-disable-next-line functional/no-throw-statements
throw new Error(
`Override is missing required "type" property. Value: "${JSON.stringify(
rawValue,
)}"`,
);
}
if (value.to === undefined) {
// eslint-disable-next-line functional/no-throw-statements
throw new Error(
`Override is missing required "to" property. Value: "${JSON.stringify(
rawValue,
)}"`,
);
}
const restKeys = Object.keys(rest);
if (restKeys.length > 0) {
// eslint-disable-next-line functional/no-throw-statements
throw new Error(
`Override is contains unknown property(s) "${restKeys.join(
", ",
)}". Value: "${JSON.stringify(rawValue)}"`,
);
}

return value;
});

const keepDefault =
Array.isArray(overridesSetting) || overridesSetting.keepDefault !== false;
Expand Down

0 comments on commit d3ce5b0

Please sign in to comment.