aboutsummaryrefslogtreecommitdiff
blob: d564ff93abda6382391366ab6e53c9a750aa6fcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
}