Defend against self-referencing composites

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-tursoandJanne Savolainen authored and Janne Savolainen committed 2022-10-20 08:39:19 +03:00
1 parent 822f4394fe
commit db774c18d9
2 files changed
+34

No files matched your search

@@ -235,6 +235,26 @@ Available parent ids are:
}); });
}); });
it("given items with same id and parent id, throws", () => {
const someItem = {
id: "some-id",
parentId: "some-id",
};
const someRoot = {
id: "root",
someParentId: undefined,
};
const items = [someItem, someRoot];
expect(() => {
getComposite({
source: items,
});
}).toThrow('Tried to get a composite, but found items with self as parent: "some-id"');
});
it("given undefined ids, throws", () => { it("given undefined ids, throws", () => {
const root = { const root = {
someParentId: undefined, someParentId: undefined,
@@ -50,6 +50,20 @@ export default <T>({
); );
} }
const selfReferencingIds = pipeline(
source,
filter((x) => getId(x) === getParentId(x)),
map(getId),
);
if (selfReferencingIds.length) {
throw new Error(
`Tried to get a composite, but found items with self as parent: "${selfReferencingIds.join(
'", ',
)}"`,
);
}
const duplicateIds = pipeline( const duplicateIds = pipeline(
source, source,
countBy(getId), countBy(getId),