handle arrays correctly in checkMatchesObjectStructure

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