« get me outta code hell

validate-writes.js « write « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write/validate-writes.js
blob: 5d61d0e71cdd6c402845abd771615f46d42a03bc (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {logError} from '../util/cli.js';

function validateWritePath(path, urlGroup) {
  if (!Array.isArray(path)) {
    return {error: `Expected array, got ${path}`};
  }

  const {paths} = urlGroup;

  const definedKeys = Object.keys(paths);
  const specifiedKey = path[0];

  if (!definedKeys.includes(specifiedKey)) {
    return {error: `Specified key ${specifiedKey} isn't defined`};
  }

  const expectedArgs = paths[specifiedKey].match(/<>/g)?.length ?? 0;
  const specifiedArgs = path.length - 1;

  if (specifiedArgs !== expectedArgs) {
    return {
      error: `Expected ${expectedArgs} arguments, got ${specifiedArgs}`,
    };
  }

  return {success: true};
}

function validateWriteObject(obj, {
  urlSpec,
}) {
  if (typeof obj !== 'object') {
    return {error: `Expected object, got ${typeof obj}`};
  }

  if (typeof obj.type !== 'string') {
    return {error: `Expected type to be string, got ${obj.type}`};
  }

  switch (obj.type) {
    case 'legacy': {
      if (typeof obj.write !== 'function') {
        return {error: `Expected write to be string, got ${obj.write}`};
      }

      break;
    }

    case 'page': {
      const path = validateWritePath(obj.path, urlSpec.localized);
      if (path.error) {
        return {error: `Path validation failed: ${path.error}`};
      }

      if (typeof obj.page !== 'function') {
        return {error: `Expected page to be function, got ${obj.content}`};
      }

      break;
    }

    case 'data': {
      const path = validateWritePath(obj.path, urlSpec.data);
      if (path.error) {
        return {error: `Path validation failed: ${path.error}`};
      }

      if (typeof obj.data !== 'function') {
        return {error: `Expected data to be function, got ${obj.data}`};
      }

      break;
    }

    case 'redirect': {
      const fromPath = validateWritePath(obj.fromPath, urlSpec.localized);
      if (fromPath.error) {
        return {
          error: `Path (fromPath) validation failed: ${fromPath.error}`,
        };
      }

      const toPath = validateWritePath(obj.toPath, urlSpec.localized);
      if (toPath.error) {
        return {error: `Path (toPath) validation failed: ${toPath.error}`};
      }

      if (typeof obj.title !== 'function') {
        return {error: `Expected title to be function, got ${obj.title}`};
      }

      break;
    }

    default: {
      return {error: `Unknown type: ${obj.type}`};
    }
  }

  return {success: true};
}

export function validateWrites(writes, {
  functionName,
  urlSpec,
}) {
  // Do a quick valid8tion! If one of the writeThingPages functions go
  // wrong, this will stall out early and tell us which did.

  if (!Array.isArray(writes)) {
    logError`${functionName} didn't return an array!`;
    return false;
  }

  if (!(
    writes.every((obj) => typeof obj === 'object') &&
    writes.every((obj) => {
      const result = validateWriteObject(obj, {
        urlSpec,
      });
      if (result.error) {
        logError`Validating write object failed: ${result.error}`;
        return false;
      } else {
        return true;
      }
    })
  )) {
    logError`${functionName} returned invalid entries!`;
    return false;
  }

  return true;
}