-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathvalidate.js
More file actions
342 lines (316 loc) · 9.11 KB
/
validate.js
File metadata and controls
342 lines (316 loc) · 9.11 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
const Utils = require("../../utils.js");
const validate = (config) => {
let errs = [];
if (config) {
errs = errs.concat(validateStructure(config));
if (errs.length === 0) {
errs = errs.concat(validateLayers(config));
}
} else errs.push(err(`Configuration object is missing.`));
if (errs.length === 0) return { valid: true };
else
return {
valid: false,
errors: errs,
};
};
const validateStructure = (config) => {
const errs = [];
if (config == null) errs.push(err(`Configuration is missing.`));
if (config.msv == null)
errs.push(
err(`Configuration is missing the 'msv' (mission-site-view) object.`, [
"msv",
])
);
if (config.layers == null)
errs.push(err(`Configuration is missing the 'layers' object.`, ["layers"]));
if (config.tools == null)
errs.push(err(`Configuration is missing the 'tools' object.`, ["tools"]));
return errs;
};
const validateLayers = (config) => {
let errs = [];
let existingUUIDs = [];
Utils.traverseLayers(config.layers, (layer) => {
// Check layer name
const validNameErrs = isValidLayerName(layer.name);
if (validNameErrs.length > 0) {
errs = errs.concat(validNameErrs);
return;
}
fillInMissingFieldsWithDefaults(layer);
switch (layer.type) {
case "header":
break;
case "tile":
// Check url
errs = errs.concat(isValidUrl(layer));
// Check zooms
errs = errs.concat(isValidZooms(layer));
break;
case "vectortile":
// Check url
errs = errs.concat(isValidUrl(layer));
// Check zooms
errs = errs.concat(isValidZooms(layer));
break;
case "data":
// Check url
errs = errs.concat(isValidDemUrl(layer));
// Check zooms
errs = errs.concat(isValidZooms(layer));
break;
case "query":
// Check url
errs = errs.concat(isValidEndpoint(layer));
break;
case "vector":
// Check url
if (layer.controlled !== true) errs = errs.concat(isValidUrl(layer));
break;
case "velocity":
// Check url
if (layer.controlled !== true) errs = errs.concat(isValidUrl(layer));
break;
case "model":
// Check url
errs = errs.concat(isValidUrl(layer));
// Check model params (pos, rot, scale)
errs = errs.concat(isValidModelParams(layer));
break;
case "image":
// Check url
errs = errs.concat(isValidUrl(layer));
// Check zooms
errs = errs.concat(isValidZooms(layer));
break;
default:
errs = errs.concat(
err(`Unknown layer type: '${layer.type}'`, ["layers[layer].type"])
);
}
if (layer.uuid != null) {
if (existingUUIDs.includes(layer.uuid)) {
errs = errs.concat([
err(
`Found a layer with duplicate uuid: ${layer.name} - ${layer.uuid}`
),
]);
} else existingUUIDs.push(layer.uuid);
}
});
return errs;
};
const isValidLayerName = (name) => {
const errs = [];
if (name == null)
errs.push(err("Found a layer with name: null.", ["layers[layer].name"]));
if (name === "")
errs.push(err("Found a layer with name: ''", ["layers[layer].name"]));
if (name === "undefined")
errs.push(
err("Found a layer with name: undefined", ["layers[layer].name"])
);
return errs;
};
const isValidUrl = (layer) => {
const errs = [];
if (layer.url == null)
errs.push(
err(`Layer '${layer.name}' has URL: null.`, ["layers[layer].url"])
);
if (layer.url === "")
errs.push(err(`Layer '${layer.name}' has URL: ''`, ["layers[layer].url"]));
if (layer.url === "undefined")
errs.push(
err(`Layer '${layer.name}' has URL: undefined`, ["layers[layer].url"])
);
return errs;
};
const isValidDemUrl = (layer) => {
const errs = [];
if (layer.demtileurl == null)
errs.push(
err(`Layer '${layer.name}' has DEM tile URL: null.`, [
"layers[layer].demtileurl",
])
);
if (layer.demtileurl === "")
errs.push(
err(`Layer '${layer.name}' has DEM tile URL: ''`, [
"layers[layer].demtileurl",
])
);
if (layer.demtileurl === "undefined")
errs.push(
err(`Layer '${layer.name}' has DEM tile URL: undefined`, [
"layers[layer].demtileurl",
])
);
return errs;
};
const isValidEndpoint = (layer) => {
const errs = [];
if (layer.query?.endpoint == null)
errs.push(
err(`Layer '${layer.name}' has Endpoint: null.`, [
"layers[layer].query.endpoint",
])
);
if (layer.query?.endpoint === "")
errs.push(
err(`Layer '${layer.name}' has Endpoint: ''`, [
"layers[layer].query.endpoint",
])
);
if (layer.query?.endpoint === "undefined")
errs.push(
err(`Layer '${layer.name}' has Endpoint: undefined`, [
"layers[layer].query.endpoint",
])
);
return errs;
};
const isValidZooms = (layer) => {
const errs = [];
if (isNaN(layer.minZoom))
errs.push(
err(`Layer '${layer.name}' has Minimum Zoom: undefined`, [
"layers[layer].minZoom",
])
);
else if (layer.minZoom < 0)
errs.push(
err(`Layer '${layer.name}' has Minimum Zoom: < 0`, [
"layers[layer].minZoom",
])
);
if (isNaN(layer.maxNativeZoom))
errs.push(
err(`Layer '${layer.name}' has Maximum Native Zoom: undefined`, [
"layers[layer].maxNativeZoom",
])
);
if (isNaN(layer.maxZoom))
errs.push(
err(`Layer '${layer.name}' has Maximum Zoom: undefined`, [
"layers[layer].maxZoom",
])
);
if (
!isNaN(layer.minZoom) &&
!isNaN(layer.maxNativeZoom) &&
!isNaN(layer.maxZoom) &&
layer.minZoom > layer.maxNativeZoom
)
errs.push(
err(`Layer '${layer.name}' has Minimum Zoom > Maximum Native Zoom`, [
"layers[layer].minZoom",
"layers[layer].maxNativeZoom",
])
);
return errs;
};
const isValidModelParams = (layer) => {
const errs = [];
if (
isNaN(layer.position?.longitude) ||
isNaN(layer.position?.latitude) ||
isNaN(layer.position?.elevation)
)
errs.push(
err(
`Layer '${layer.name}' has invalid Longitude, Latitude or Elevation. Defaulting to 0.`,
[
"layers[layer].position.longitude",
"layers[layer].position.latitude",
"layers[layer].position.elevation",
],
true
)
);
if (
isNaN(layer.rotation?.x) ||
isNaN(layer.rotation?.y) ||
isNaN(layer.rotation?.z)
)
errs.push(
err(
`Layer '${layer.name}' has invalid Rotation X, Y or Z. Defaulting to 0.`,
[
"layers[layer].rotation.x",
"layers[layer].rotation.y",
"layers[layer].rotation.z",
],
true
)
);
if (isNaN(layer.scale))
errs.push(
err(
`Layer '${layer.name}' has invalid Scale. Defaulting to 0.`,
["layers[layer].scale"],
true
)
);
return errs;
};
const hasNonHeaderWithSublayers = (config) => {
const errs = [];
Utils.traverseLayers(config.layers, (layer) => {
if (layer.type !== "header" && layer.sublayers != null)
errs.push(
err(`Non-header layer '${layer.name}' has sublayers.`, [
"layers[non-header-layer].!sublayer",
])
);
});
return errs;
};
const fillInMissingFieldsWithDefaults = (layer) => {
if (layer.type != "header") {
layer.initialOpacity =
layer.initialOpacity == null ? 1 : layer.initialOpacity;
layer.visibility = layer.visibility == null ? true : layer.visibility;
}
switch (layer.type) {
case "header":
break;
case "tile":
layer.tileformat = layer.tileformat == null ? "tms" : layer.tileformat;
break;
case "vectortile":
layer.style = layer.style || {};
layer.style.className = layer.name.replace(/ /g, "").toLowerCase();
break;
case "data":
layer.style = layer.style || {};
layer.style.className = layer.name.replace(/ /g, "").toLowerCase();
layer.tileformat = layer.tileformat == null ? "tms" : layer.tileformat;
break;
case "query":
layer.style = layer.style || {};
layer.style.className = layer.name.replace(/ /g, "").toLowerCase();
break;
case "vector":
layer.style = layer.style || {};
layer.style.className = layer.name.replace(/ /g, "").toLowerCase();
break;
case "image":
layer.style = layer.style || {};
layer.style.className = layer.name.replace(/ /g, "").toLowerCase();
break;
case "model":
break;
default:
}
};
const err = (reason, invalidFields, onlyAWarning) => {
return {
type: onlyAWarning ? "warning" : "error",
reason: reason,
invalidFields: invalidFields || [],
};
};
module.exports = validate;