-
Notifications
You must be signed in to change notification settings - Fork 0
/
validatex3djson.js
65 lines (60 loc) · 1.96 KB
/
validatex3djson.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
var fs = require('fs');
const Ajv2020 = require("ajv/dist/2020");
const ajv = new Ajv2020({ strict: false });
const addFormats = require("ajv-formats");
var selectObjectFromJson = require('./selectObjectFromJson');
addFormats(ajv);
var schema = JSON.parse(fs.readFileSync('x3d-4.0-JSONSchema.json').toString());
// var schema = JSON.parse(fs.readFileSync('X3dXml4.0SchemaConvertedToJson2020-12Schema.json').toString());
function validateFile(file) {
try {
var json = JSON.parse(fs.readFileSync(file).toString());
var version = "4.0";
try {
var valid = ajv.validate(schema, json);
if (ajv.errors !== null) {
var errs = ajv.errors;
for (var e in errs) {
if (e == 0) {
console.log(e, 'Ajv '+version+' Validation failed on', file);
}
var error = "";
error += "\r\n keyword: " + errs[e].keyword + "\r\n";
var instancePath = errs[e].instancePath.replace(/^\./, "").replace(/[\.\[\]']+/g, " > ").replace(/ >[ \t]*$/, "");
error += " instancePath: " + instancePath+ "\r\n";
var selectedObject = selectObjectFromJson(json, instancePath);
error += " value: " + JSON.stringify(selectedObject,
function(k, v) {
var v2 = JSON.parse(JSON.stringify(v));
if (typeof v2 === 'object') {
for (var o in v2) {
if (typeof v2[o] === 'object') {
v2[o] = "|omitted|";
}
}
}
return v2;
}) + "\r\n";
error += " message: " + errs[e].message + "\r\n";
error += " params: " + JSON.stringify(errs[e].params) + "\r\n";
error += " file: " + file + "\r\n";
error += " version: " + version + "\r\n";
console.log(error);
}
} else {
console.log('Successful', file);
}
} catch (e) {
console.log('Failed validation', file, e);
}
} catch (e) {
console.log('Failed JSON parse', file, e);
}
}
process.argv.shift();
process.argv.shift();
for (i in process.argv) {
var file = process.argv[i];
validateFile(file);
}
process.exit();