-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (61 loc) · 1.47 KB
/
Copy pathindex.js
File metadata and controls
75 lines (61 loc) · 1.47 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
#!/usr/bin/env node
const Ajv = require("ajv");
const ajv = new Ajv({ allErrors: true });
const fs = require("fs");
const path = require("path");
// Load Schemas
const gameSchema = require("./schemas/game.schema.json");
const themeSchema = require("./schemas/theme.schema.json");
const configSchema = require("./schemas/config.schema.json");
const tilesSchema = require("./schemas/tiles.schema.json");
// Validate and load schemas
ajv.addSchema([gameSchema, themeSchema, configSchema, tilesSchema]);
const determineSchema = (json) => {
// Games have an info object first thing
if (json.info) {
return gameSchema.$id;
}
// Theme files have a colors field
if (json.colors) {
return themeSchema.$id;
}
// Config files have a theme setting
if (json.theme) {
return configSchema.$id;
}
// Tiles are just collections of tiles so they are the default
return tilesSchema.$id;
};
let validate = (json, file) => {
const id = determineSchema(json);
const valid = ajv.validate(id, json);
return {
valid,
id,
file,
validationErrors: ajv.errors,
};
};
validate.file = (file) => {
if (!fs.existsSync(file)) {
return {
valid: false,
id: "unknown",
file,
error: "file not found",
};
}
let json;
try {
json = require(file);
} catch (err) {
return {
valid: false,
id: "unknown",
file,
error: err.message,
};
}
return validate(json, file);
};
module.exports = validate;