From f5dec41b34ff17c79a334d7960b8775abf7da4f1 Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Tue, 6 May 2025 14:57:58 +0100 Subject: [PATCH] fix: handle array edge case --- api/src/utils/helpers/flatten.spec.ts | 4 ++++ api/src/utils/helpers/flatten.ts | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/api/src/utils/helpers/flatten.spec.ts b/api/src/utils/helpers/flatten.spec.ts index aa3ffac5..dbb5f9ce 100644 --- a/api/src/utils/helpers/flatten.spec.ts +++ b/api/src/utils/helpers/flatten.spec.ts @@ -111,4 +111,8 @@ describe('flatten', () => { expect(result).toStrictEqual({}); }); + + it('should throw an error if data is an array', () => { + expect(() => flatten([])).toThrow('Data should be an object!'); + }); }); diff --git a/api/src/utils/helpers/flatten.ts b/api/src/utils/helpers/flatten.ts index 466d76ee..adf517a9 100644 --- a/api/src/utils/helpers/flatten.ts +++ b/api/src/utils/helpers/flatten.ts @@ -18,6 +18,10 @@ export const flatten = ( prefix: string | undefined = undefined, result: object = {}, ): object => { + if (Array.isArray(data)) { + throw new Error('Data should be an object!'); + } + for (const [key, value] of Object.entries(data)) { const path = prefix ? `${prefix}.${key}` : key;