forked from spdx/license-list-XML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-schema.js
executable file
·73 lines (69 loc) · 1.7 KB
/
validate-schema.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
#!/usr/bin/env node
/**
* Node.js for validating SPDX license XML files.
* SPDX-License-Identifier: MIT
*/
var xsd = require('libxml-xsd');
var glob = require('glob');
var fs = require('fs');
/**
* Validates a files against the XSD
* @param file
* @returns Error if any error occurs, otherwise returns null
*/
function validate(schema, file) {
var documentString = fs.readFileSync(file, 'utf8');
try {
var validationErrors = schema.validate(documentString);
} catch(error) {
return new Error('File ' + file + ': ' + error);
}
if (validationErrors) {
var errormsg = null;
validationErrors.forEach(function(error) {
if (errormsg) {
errormsg += ';';
errormsg += ' at line ' + error.line + ' ' + error.message;
} else {
errormsg = ' at line ' + error.line + ' ' + error.message;
}
});
return new Error("File "+file+" "+errormsg);
} else {
return null; // no errors
}
}
function main() {
var srcDir = 'src';
var xsdLocation = 'schema/ListedLicense.xsd';
var schemaString = fs.readFileSync(xsdLocation, 'utf8');
var schema = xsd.parse(schemaString);
var files = process.argv.filter(function(arg) {
return arg.endsWith('.xml');
});
if (files.length == 0) {
files = glob.sync('./' + srcDir + '/**/*.xml');
}
var error = null;
var pass = 0, fail = 0;
files.forEach(function(file) {
var fileError = validate(schema, file);
if (fileError) {
fail++;
if (!error) {
error = fileError;
} else {
// append the file in error
error = new Error(error.message+fileError.message);
}
} else {
pass++;
}
});
console.log('validation complete ' + pass + ' passed, ' + fail + ' failed');
if (error) {
console.error(error.message);
process.exit(1);
}
}
main();