-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_integrity.js
113 lines (97 loc) · 2.61 KB
/
test_integrity.js
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
const fs = require("fs");
const { join } = require("path");
const pathToDB = join(__dirname, "db.json");
const db = Object.values(JSON.parse(fs.readFileSync(pathToDB)));
let errorMessages = [];
let evaluatedIds = {};
const CATEGORIES = ["course", "resource", "template", "project"];
const LANGUAGES = ["en", "fr"];
const isValidHTTPURL = (str) =>
null !==
str.match(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/i
);
db.forEach((el) => {
if (!el.id) {
errorMessages.push(`Article has no ID : ${JSON.stringify(el, null, 2)}`);
}
if (!el.title || typeof el.title !== "string" || el.title.trim() === "") {
errorMessages.push(
`Article has an empty title or no title : ${JSON.stringify(el, null, 2)}`
);
}
if (evaluatedIds[el.id]) {
errorMessages.push(
`You have multiple articles with the same ID : ${JSON.stringify(
el,
null,
2
)} \n ${JSON.stringify(evaluatedIds[el.id], null, 2)}`
);
}
if (!el.url || !isValidHTTPURL(el.url)) {
errorMessages.push(
`Article has an invalid http(s) URL or no URL : ${JSON.stringify(
el,
null,
2
)}`
);
}
if (!isValidHTTPURL(el.thumbnail) && el.thumbnail.trim() !== "") {
errorMessages.push(
`The URL of the thumbnail is malformed : ${JSON.stringify(el, null, 2)}`
);
}
if (!el.language) {
errorMessages.push(
`The article dosen't specify a language : ${JSON.stringify(el, null, 2)}`
);
}
//some partial salvage
el.language = el.language.toLowerCase();
if (!LANGUAGES.includes(el.language)) {
errorMessages.push(
`The article specifies an unsupported language: \n supported languages ${LANGUAGES} \n ${JSON.stringify(
el,
null,
2
)}`
);
}
if (!el.type) {
errorMessages.push(
`The article dosen't specify a category : ${JSON.stringify(el, null, 2)}`
);
}
//some partial salvage
el.type = el.type.toLowerCase();
if (!CATEGORIES.includes(el.type)) {
errorMessages.push(
`The article specifies an unsupported category: \n supported categories ${CATEGORIES} \n ${JSON.stringify(
el,
null,
2
)}`
);
}
//##################
//
//start of salvaging
//
//##################
evaluatedIds[el.id] = el;
if (!el.description) {
el.description = "";
}
if (!el.tags) {
el.tags = [];
}
if (!el.thumbnail) {
el.thumbnail = "";
}
});
if (errorMessages.length > 0) {
throw errorMessages.join("\n");
}
fs.writeFileSync(pathToDB, JSON.stringify(db, null, 2));