-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
311 lines (273 loc) · 8.36 KB
/
validate.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
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
import { exec } from 'node:child_process';
import { Validator } from '@cfworker/json-schema';
import chalk from 'chalk';
import fsp from 'node:fs/promises';
import papaparse from 'papaparse';
import path from 'node:path';
import url from 'node:url';
import xsdValidator from 'xsd-validator';
const validateSchema = xsdValidator.default;
import { XMLValidator } from 'fast-xml-parser';
const Glyph = {
CROSS_MARK: '\u{274C}',
OK_SIGN: '\u{1F197}',
WARNING_SIGN: '\u{26A0}\u{FE0F} ',
WHITE_HEAVY_CHECK_MARK: '\u{2705}',
};
const Status = {
VALID: 'valid',
INVALID: 'invalid',
MALFORMED: 'malformed',
};
let jsonValidator;
let rawSchema;
/**
* Get the working directory, based on where the script is being called (as
* opposed to where it is located).
*
* @returns {Promise<string>} - an absolute path
*/
function pwd() {
return new Promise((resolve, reject) => {
exec('pwd', (err, res) => {
if (err) {
reject(err);
} else {
resolve(res.trim());
}
});
});
}
/**
* Recurse down an object’s hierarchy and build up an array of all values and
* array entries that look like HTML fragments.
*
* @param {any} arg
* @returns {[string]} - the HTML fragments
*/
export function findHtmlFragments(arg) {
if (typeof arg === 'string' && /<.+?>/.test(arg)) {
// Base case: only return an array if this string looks like an HTML fragment.
return [arg];
} else if (typeof arg === 'object') {
// If arg is an array, run findHtmlFragments on all of its members; if
// it’s an object, run findHtmlFragments on all of its values.
const arr = Array.isArray(arg) ? arg : Object.values(arg);
let results = [];
for (const entry of arr) {
const result = findHtmlFragments(entry);
if (Array.isArray(result) && result.length > 0) {
results = results.concat(result);
}
}
return results;
}
}
async function writeDataToReportCsv(filepath, data) {
if (Array.isArray(data) && data.length === 0) {
return;
}
// create directory (and any sub directories) if it doesn't exist
const { dir } = path.parse(filepath);
await fsp.mkdir(dir, { recursive: true });
// write out the data
const unparsed = papaparse.unparse(data, { newline: '\n', header: true });
await fsp.writeFile(filepath, unparsed);
}
async function validateJson(json) {
// Load the JSON schema and create the validator.
if (!jsonValidator) {
const schemaPath = url.fileURLToPath(
import.meta.resolve('#article.schema.json'),
);
const schemaStr = await fsp.readFile(schemaPath);
const schema = JSON.parse(schemaStr);
jsonValidator = new Validator(schema);
}
const { valid, errors } = jsonValidator.validate(json);
if (valid) {
return {
status: Status.VALID,
};
} else {
return {
status: Status.INVALID,
errors: errors.map(({ error }) => error),
};
}
}
async function validateRaw(raw) {
if (!rawSchema) {
const schemaPath = url.fileURLToPath(
import.meta.resolve('#article.raw.schema.xsd'),
);
rawSchema = await fsp.readFile(schemaPath);
}
try {
const result = validateSchema(raw, rawSchema);
if (result === true) {
return {
status: Status.VALID,
};
} else {
return {
status: Status.INVALID,
errors: result.map((err) => err.message.trim()),
};
}
} catch (err) {
return {
status: Status.MALFORMED,
errors: [err.message.trim()],
};
}
}
function validateHtmlFragment(htmlFragment) {
const xml = `<div>${htmlFragment}</div>`;
const result = XMLValidator.validate(xml);
if (result === true) {
return {
status: Status.VALID,
};
} else {
return {
status: Status.INVALID,
content: htmlFragment,
errors: result.err.msg,
};
}
}
// NOTE: jsonld is the only value inside the schema that can have HTML fragments
function validateHtmlFragments(jsonld) {
const htmlFragments = findHtmlFragments(jsonld);
let errors = [];
let anyIsInvalid = false;
for (const htmlFragment of htmlFragments) {
const validationResult = validateHtmlFragment(htmlFragment);
if (validationResult.status === Status.INVALID) {
anyIsInvalid = anyIsInvalid || true;
}
errors = errors.concat(validationResult.errors || []);
}
return {
status: anyIsInvalid ? Status.INVALID : Status.VALID,
errors,
};
}
async function generateFileReport(jsonFilePath) {
const jsonString = await fsp.readFile(jsonFilePath);
const { base: filename } = path.parse(jsonFilePath);
const report = {
path: jsonFilePath,
filename,
overall_status: '',
json_status: '',
raw_status: '',
html_fragments_status: '',
json_errors: [],
raw_errors: [],
html_fragments_errors: [],
};
let json;
try {
json = JSON.parse(jsonString);
} catch (err) {
report.overall_status = Status.MALFORMED;
report.json_status = Status.MALFORMED;
report.json_errors = err;
return report;
}
const jsonValidationResult = await validateJson(json);
report.json_status = jsonValidationResult.status;
report.json_errors = jsonValidationResult.errors || [];
const rawValidationResult = await validateRaw(json.raw);
report.raw_status = rawValidationResult.status;
report.raw_errors = rawValidationResult.errors || [];
const htmlFragmentsValidationResult = validateHtmlFragments(json.jsonld);
report.html_fragments_status = htmlFragmentsValidationResult.status;
report.html_fragments_errors = htmlFragmentsValidationResult.errors || [];
// The overall status is only valid if everything was valid.
if (
report.json_status === Status.VALID &&
report.raw_status === Status.VALID &&
report.html_fragments_status === Status.VALID
) {
report.overall_status = Status.VALID;
} else {
report.overall_status = Status.INVALID;
}
return report;
}
/**
* Find all of the JSON files at a given `inputPath`.
* If the input is a directory, include all of the JSON files in that directory.
* If the input is a file, include just the file.
*
* @param {string} inputPath
* @returns
*/
async function getJsonFilePathsFromInputPath(inputPath) {
let jsonFilePaths = [];
const inputStats = await fsp.stat(inputPath);
if (inputStats.isDirectory()) {
try {
const entries = await fsp.readdir(inputPath, {
withFileTypes: true,
recursive: true,
});
jsonFilePaths = entries
.filter((entry) => entry.isFile() && entry.name.endsWith('json'))
.map((entry) => path.resolve(entry.parentPath, entry.name));
} catch (err) {
console.error(err.message);
}
} else if (inputStats.isFile() && inputPath.endsWith('.json')) {
jsonFilePaths.push(inputPath);
}
return jsonFilePaths;
}
(async function main() {
const input = process.argv?.[2];
if (!input) {
console.error(
'One positional argument is required (a path to a directory or file).',
);
process.exit(1);
}
// Build an absolute path based on the input.
let inputPath = input;
if (!path.isAbsolute(input)) {
const workingDir = await pwd();
inputPath = path.resolve(workingDir, input);
console.log(inputPath);
}
const jsonFilePaths = await getJsonFilePathsFromInputPath(inputPath);
// There’s nothing to validate, so bail out early.
if (jsonFilePaths.length === 0) {
console.error('Could not find any JSON files in input.');
process.exit(1);
}
const rows = [];
for (const jsonFilePath of jsonFilePaths) {
const report = await generateFileReport(jsonFilePath);
const shortPath = jsonFilePath.replace(inputPath + '/', '');
if (report.overall_status === Status.VALID) {
console.log(Glyph.WHITE_HEAVY_CHECK_MARK + ' ' + chalk.green(shortPath));
} else if (report.overall_status === Status.INVALID) {
console.log(Glyph.CROSS_MARK + ' ' + chalk.red(shortPath));
} else if (report.overall_status === Status.MALFORMED) {
console.log(Glyph.WARNING_SIGN + ' ' + chalk.yellow(shortPath));
}
rows.push(report);
}
// Write the report
const timestamp = new Date().toISOString().replace(/(:|\.)/g, '');
const reportPath = url.fileURLToPath(
import.meta.resolve(`#reports/${timestamp}.csv`),
);
writeDataToReportCsv(reportPath, rows);
console.log('\nKey');
console.log(`${Glyph.WHITE_HEAVY_CHECK_MARK} = valid`);
console.log(`${Glyph.CROSS_MARK} = invalid`);
console.log(`${Glyph.WARNING_SIGN} = malformed`);
})();