perform checkMatchesObjectStructure in parseValidObject

This commit is contained in:
marlon-erler
2024-07-22 01:40:00 +02:00
parent d85ced4e36
commit 53399ba2b4
4 changed files with 25 additions and 18 deletions
+9 -2
View File
@@ -1,4 +1,4 @@
import { checkIsValidObject } from "./typeSafety"; import { checkIsValidObject, checkMatchesObjectStructure } from "./typeSafety";
// date // date
export function createTimestamp(): string { export function createTimestamp(): string {
@@ -18,9 +18,16 @@ export function parse(string: string): any {
} }
} }
export function parseValidObject<T>(string: string): T|null { export function parseValidObject<T>(string: string, reference: T): T | null {
const parsed: any = parse(string); const parsed: any = parse(string);
if (checkIsValidObject(parsed) == false) return null; if (checkIsValidObject(parsed) == false) return null;
const doesMatchReference: boolean = checkMatchesObjectStructure(
parsed,
reference
);
if (doesMatchReference == false) return null;
return parsed; return parsed;
} }
+5 -5
View File
@@ -1,9 +1,6 @@
// this file is responsible for managing chats. // this file is responsible for managing chats.
import { import { DATA_VERSION, ValidObject } from "./Utility/typeSafety";
DATA_VERSION,
ValidObject,
} from "./Utility/typeSafety";
import FileModel, { File } from "./fileModel"; import FileModel, { File } from "./fileModel";
import StorageModel, { storageKeys } from "./storageModel"; import StorageModel, { storageKeys } from "./storageModel";
import { import {
@@ -182,7 +179,10 @@ export default class ChatModel {
}; };
handleMessage = (body: string): void => { handleMessage = (body: string): void => {
const chatMessage: ChatMessage | null = parseValidObject<ChatMessage>(body); const chatMessage: ChatMessage | null = parseValidObject(
body,
ChatMessageReference
);
if (chatMessage == null) return; if (chatMessage == null) return;
chatMessage.status = ChatMessageStatus.Received; chatMessage.status = ChatMessageStatus.Received;
+1 -1
View File
@@ -13,7 +13,7 @@ export default class FileModel {
// handler // handler
handleStringifiedFile = (stringifiedFile: string): void => { handleStringifiedFile = (stringifiedFile: string): void => {
const file: File | null = parseValidObject<File>(stringifiedFile); const file: File | null = parseValidObject(stringifiedFile, FileReference);
if (file == null) return; if (file == null) return;
this.storeFile(file); this.storeFile(file);
+10 -10
View File
@@ -1,11 +1,11 @@
// this file is responsible for reading and writing persistent data; all storage shall be handled by this file. // 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 { import {
localeCompare, DATA_VERSION,
parseValidObject, ValidObject,
stringify, checkMatchesObjectStructure,
} from "./Utility/utility"; } from "./Utility/typeSafety";
import { localeCompare, parseValidObject, stringify } from "./Utility/utility";
export const PATH_COMPONENT_SEPARATOR = "\\"; export const PATH_COMPONENT_SEPARATOR = "\\";
@@ -77,16 +77,16 @@ export default class StorageModel {
this.write(pathComponents, valueString); this.write(pathComponents, valueString);
}; };
readStringifiable = <T extends ValidObject>(pathComponents: string[], reference: T): T | null => { readStringifiable = <T extends ValidObject>(
pathComponents: string[],
reference: T
): T | null => {
const valueString: string | null = this.read(pathComponents); const valueString: string | null = this.read(pathComponents);
if (!valueString) return null; if (!valueString) return null;
const object: any | null = parseValidObject(valueString); const object: any | null = parseValidObject(valueString, reference);
if (object == null) return null; if (object == null) return null;
const doesMatchReference: boolean = checkMatchesObjectStructure(object, reference);
if (doesMatchReference == false) return null;
return object; return object;
}; };