From 53399ba2b40fdd2b19599d973d2dbb5566dda09e Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Mon, 22 Jul 2024 01:40:00 +0200 Subject: [PATCH] perform checkMatchesObjectStructure in parseValidObject --- src/Model/Utility/utility.ts | 11 +++++++++-- src/Model/chatModel.ts | 10 +++++----- src/Model/fileModel.ts | 2 +- src/Model/storageModel.ts | 20 ++++++++++---------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Model/Utility/utility.ts b/src/Model/Utility/utility.ts index f7ec6386..8b3a9f5a 100644 --- a/src/Model/Utility/utility.ts +++ b/src/Model/Utility/utility.ts @@ -1,4 +1,4 @@ -import { checkIsValidObject } from "./typeSafety"; +import { checkIsValidObject, checkMatchesObjectStructure } from "./typeSafety"; // date export function createTimestamp(): string { @@ -18,9 +18,16 @@ export function parse(string: string): any { } } -export function parseValidObject(string: string): T|null { +export function parseValidObject(string: string, reference: T): T | null { const parsed: any = parse(string); if (checkIsValidObject(parsed) == false) return null; + + const doesMatchReference: boolean = checkMatchesObjectStructure( + parsed, + reference + ); + if (doesMatchReference == false) return null; + return parsed; } diff --git a/src/Model/chatModel.ts b/src/Model/chatModel.ts index a32aa139..97f50555 100644 --- a/src/Model/chatModel.ts +++ b/src/Model/chatModel.ts @@ -1,9 +1,6 @@ // this file is responsible for managing chats. -import { - DATA_VERSION, - ValidObject, -} from "./Utility/typeSafety"; +import { DATA_VERSION, ValidObject } from "./Utility/typeSafety"; import FileModel, { File } from "./fileModel"; import StorageModel, { storageKeys } from "./storageModel"; import { @@ -182,7 +179,10 @@ export default class ChatModel { }; handleMessage = (body: string): void => { - const chatMessage: ChatMessage | null = parseValidObject(body); + const chatMessage: ChatMessage | null = parseValidObject( + body, + ChatMessageReference + ); if (chatMessage == null) return; chatMessage.status = ChatMessageStatus.Received; diff --git a/src/Model/fileModel.ts b/src/Model/fileModel.ts index 7b2f2246..b38c18ff 100644 --- a/src/Model/fileModel.ts +++ b/src/Model/fileModel.ts @@ -13,7 +13,7 @@ export default class FileModel { // handler handleStringifiedFile = (stringifiedFile: string): void => { - const file: File | null = parseValidObject(stringifiedFile); + const file: File | null = parseValidObject(stringifiedFile, FileReference); if (file == null) return; this.storeFile(file); diff --git a/src/Model/storageModel.ts b/src/Model/storageModel.ts index 5774512a..a2d66c99 100644 --- a/src/Model/storageModel.ts +++ b/src/Model/storageModel.ts @@ -1,11 +1,11 @@ // this file is responsible for reading and writing persistent data; all storage shall be handled by this file. -import { DATA_VERSION, ValidObject, checkMatchesObjectStructure } from "./Utility/typeSafety"; import { - localeCompare, - parseValidObject, - stringify, -} from "./Utility/utility"; + DATA_VERSION, + ValidObject, + checkMatchesObjectStructure, +} from "./Utility/typeSafety"; +import { localeCompare, parseValidObject, stringify } from "./Utility/utility"; export const PATH_COMPONENT_SEPARATOR = "\\"; @@ -77,16 +77,16 @@ export default class StorageModel { this.write(pathComponents, valueString); }; - readStringifiable = (pathComponents: string[], reference: T): T | null => { + readStringifiable = ( + pathComponents: string[], + reference: T + ): T | null => { const valueString: string | null = this.read(pathComponents); if (!valueString) return null; - const object: any | null = parseValidObject(valueString); + const object: any | null = parseValidObject(valueString, reference); if (object == null) return null; - const doesMatchReference: boolean = checkMatchesObjectStructure(object, reference); - if (doesMatchReference == false) return null; - return object; };