From f2ecc1803f3ea294a0c6b7915b61348ed0395b26 Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Wed, 16 Nov 2022 15:16:38 +0100 Subject: Remade and extended the app using React --- src/models/index.guard.ts | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/models/index.guard.ts (limited to 'src/models/index.guard.ts') diff --git a/src/models/index.guard.ts b/src/models/index.guard.ts new file mode 100644 index 0000000..d564ff9 --- /dev/null +++ b/src/models/index.guard.ts @@ -0,0 +1,58 @@ +import { Trip, Group, MediaItem } from './index'; + +export function isGroup(obj: unknown): obj is Group { + const typedObj = obj as Group; + const isValid = + ((typedObj !== null && typeof typedObj === 'object') || typeof typedObj === 'function') && + typeof typedObj.id === 'string' && + (typeof typedObj.name === 'undefined' || typeof typedObj.name === 'string') && + (typeof typedObj.description === 'undefined' || typeof typedObj.description === 'string') && + Array.isArray(typedObj.media) && + typedObj.media.every((e: any) => isMediaItem(e)); + + if (!isValid) { + throw new Error(`Invalid object: ${JSON.stringify(obj)}`); + } + + return isValid; +} + +export function isMediaItem(obj: unknown): obj is MediaItem { + const typedObj = obj as MediaItem; + const isValid = + ((typedObj !== null && typeof typedObj === 'object') || typeof typedObj === 'function') && + typeof typedObj.name === 'string' && + typeof typedObj.src === 'string' && + typeof typedObj.type === 'string' && + (typedObj.type === 'photo' || typedObj.type === 'video') && + typeof typedObj.width === 'number' && + typeof typedObj.height === 'number' && + (typeof typedObj.latitude === 'number' || typeof typedObj.latitude === 'undefined') && + (typeof typedObj.longitude === 'number' || typeof typedObj.longitude === 'undefined') && + (typeof typedObj.time === 'undefined' || typeof typedObj.time === 'string') && + (typeof typedObj.thumbnail === 'string' || typeof typedObj.thumbnail === 'undefined') && + (typeof typedObj.caption === 'undefined' || typeof typedObj.caption === 'string'); + + if (!isValid) { + throw new Error(`Invalid object: ${JSON.stringify(obj)}`); + } + + return isValid; +} + +export function isTrip(obj: unknown): obj is Trip { + const typedObj = obj as Trip; + const isValid = + ((typedObj !== null && typeof typedObj === 'object') || typeof typedObj === 'function') && + typeof typedObj.id === 'string' && + typeof typedObj.name === 'string' && + (typeof typedObj.groups === 'undefined' || + (Array.isArray(typedObj.groups) && typedObj.groups.every((e: any) => isGroup(e))) + ); + + if (!isValid) { + throw new Error(`Invalid object: ${JSON.stringify(obj)}`); + } + + return isValid; +} -- cgit v1.2.3