perform checkMatchesObjectStructure in parseValidObject
This commit is contained in:
@@ -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<T>(string: string): T|null {
|
||||
export function parseValidObject<T>(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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ChatMessage>(body);
|
||||
const chatMessage: ChatMessage | null = parseValidObject(
|
||||
body,
|
||||
ChatMessageReference
|
||||
);
|
||||
if (chatMessage == null) return;
|
||||
|
||||
chatMessage.status = ChatMessageStatus.Received;
|
||||
|
||||
@@ -13,7 +13,7 @@ export default class FileModel {
|
||||
|
||||
// handler
|
||||
handleStringifiedFile = (stringifiedFile: string): void => {
|
||||
const file: File | null = parseValidObject<File>(stringifiedFile);
|
||||
const file: File | null = parseValidObject(stringifiedFile, FileReference);
|
||||
if (file == null) return;
|
||||
|
||||
this.storeFile(file);
|
||||
|
||||
+10
-10
@@ -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 = <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);
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user