add checkMatchesObjectStructure utility function

This commit is contained in:
marlon-erler
2024-07-22 01:11:50 +02:00
parent 42619fb017
commit 40a608af45
2 changed files with 55 additions and 4 deletions
+23 -2
View File
@@ -1,9 +1,30 @@
export const DATA_VERSION = "v2"
export const DATA_VERSION = "v2";
export function checkIsValidObject(object: any): boolean {
return object.dataVersion == DATA_VERSION;
}
export function checkMatchesObjectStructure(
objectToCheck: any,
reference: any
): boolean {
for (const key of Object.keys(reference)) {
const requiredType = typeof reference[key];
const actualType = typeof objectToCheck[key];
if (requiredType != actualType) return false;
// recurse into objects
if (requiredType != "object") continue;
const doesNestedObjectMatch = checkMatchesObjectStructure(
reference[key],
objectToCheck[key]
);
if (doesNestedObjectMatch == false) return false;
}
return true;
}
export interface ValidObject {
dataVersion: "v2";
}
}