2014-04-28 11:51:16 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2014-04-18 17:11:43 +02:00
|
|
|
var N3 = require('n3'), fs = require('fs'), N3Util = N3.Util;
|
|
|
|
|
2014-04-22 11:53:53 +02:00
|
|
|
if (!process.argv[2]) {
|
2014-04-18 17:11:43 +02:00
|
|
|
console.log('RDF N3/NTriples/Turtle file validator');
|
2014-04-28 11:51:16 +02:00
|
|
|
console.log('Usage: $ ttl <path-to-file>');
|
2014-04-18 17:11:43 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2014-04-22 11:53:53 +02:00
|
|
|
console.log('Validating ' + process.argv[2]);
|
2014-04-18 17:11:43 +02:00
|
|
|
|
2014-04-22 11:53:53 +02:00
|
|
|
var parser = N3.Parser(), turtleStream = fs.createReadStream(process.argv[2]);
|
2014-04-18 17:11:43 +02:00
|
|
|
|
2014-04-22 11:53:53 +02:00
|
|
|
var errorCount = 0, warningCount = 0;
|
2014-04-18 17:11:43 +02:00
|
|
|
|
2014-04-22 11:53:53 +02:00
|
|
|
var regexp = {
|
|
|
|
'dateTime' : /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])?T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)??(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?$/,
|
|
|
|
'double' : /[-+]?\d*([.]\d+)?/,
|
|
|
|
'float' : /[-+]?\d*[.]\d+/,
|
|
|
|
'int' : /^[-+]?(0|[1-9]\d*)$/
|
|
|
|
};
|
|
|
|
|
|
|
|
parser.parse(turtleStream, function(error, triple, prefixes) {
|
|
|
|
if (error) {
|
2014-04-18 17:11:43 +02:00
|
|
|
console.log(error);
|
2014-04-22 11:53:53 +02:00
|
|
|
errorCount++;
|
2014-04-18 17:11:43 +02:00
|
|
|
}
|
|
|
|
|
2014-04-22 11:53:53 +02:00
|
|
|
if (triple) {
|
|
|
|
if (N3Util.isLiteral(triple.object)) {
|
2014-04-18 17:11:43 +02:00
|
|
|
var value = N3Util.getLiteralValue(triple.object);
|
|
|
|
var type = N3Util.getLiteralType(triple.object);
|
|
|
|
|
2014-04-22 11:53:53 +02:00
|
|
|
type = type.replace('http://www.w3.org/2001/XMLSchema#', '');
|
|
|
|
|
|
|
|
if (regexp[type] && !regexp[type].test(value)) {
|
|
|
|
console.log('WARNING: xsd:', type, 'does not validate for literal. {', triple.subject, triple.predicate, triple.object, '}');
|
|
|
|
warningCount++;
|
2014-04-18 17:11:43 +02:00
|
|
|
}
|
2014-04-22 11:53:53 +02:00
|
|
|
}
|
2014-04-18 17:11:43 +02:00
|
|
|
} else {
|
2014-04-22 11:53:53 +02:00
|
|
|
console.log('Validation done:', errorCount, 'errors and', warningCount ,'warnings found.');
|
2014-04-18 17:11:43 +02:00
|
|
|
}
|
|
|
|
});
|