handle arrays correctly in checkMatchesObjectStructure

This commit is contained in:
marlon-erler
2024-07-22 01:19:50 +02:00
parent 48977d7562
commit cc032ad76f
+17 -7
View File
@@ -13,13 +13,23 @@ export function checkMatchesObjectStructure(
const actualType = typeof objectToCheck[key]; const actualType = typeof objectToCheck[key];
if (requiredType != actualType) return false; if (requiredType != actualType) return false;
// recurse into objects if (Array.isArray(reference[key])) {
if (requiredType != "object") continue; // only check if array is not empty
const doesNestedObjectMatch = checkMatchesObjectStructure( if (objectToCheck[key].length == 0) continue;
reference[key],
objectToCheck[key] // check first item of array
); const requiredType = typeof reference[key][0];
if (doesNestedObjectMatch == false) return false; const actualType = typeof objectToCheck[key][0];
if (requiredType != actualType) return false;
} else if (requiredType == "object") {
// recurse into objects
const doesNestedObjectMatch = checkMatchesObjectStructure(
reference[key],
objectToCheck[key]
);
if (doesNestedObjectMatch == false) return false;
}
} }
return true; return true;