2018-06-14 13:19:16 +02:00
|
|
|
var N3 = require('n3');
|
2015-07-14 18:16:11 +02:00
|
|
|
|
|
|
|
var validate = function (turtleStream, callback) {
|
2018-06-14 13:19:16 +02:00
|
|
|
var parser = N3.Parser({ format: 'text/turtle' });
|
2015-07-14 18:16:11 +02:00
|
|
|
var errorCount = 0, warningCount = 0;
|
|
|
|
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*)$/
|
|
|
|
};
|
|
|
|
|
|
|
|
var feedback = { warnings : [], errors : []};
|
2018-06-14 13:19:16 +02:00
|
|
|
|
2015-07-14 18:16:11 +02:00
|
|
|
parser.parse(turtleStream, function(error, triple, prefixes) {
|
|
|
|
if (error) {
|
2018-06-14 13:19:16 +02:00
|
|
|
feedback.errors.push(error.message);
|
2015-07-14 18:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (triple) {
|
2018-06-14 13:19:16 +02:00
|
|
|
if (triple.object.termType === 'literal') {
|
|
|
|
var value = triple.object.value;
|
|
|
|
var type = triple.object.datatype;
|
2015-07-14 18:16:11 +02:00
|
|
|
|
|
|
|
type = type.replace('http://www.w3.org/2001/XMLSchema#', '');
|
|
|
|
if (regexp[type] && !regexp[type].test(value)) {
|
2018-06-14 13:19:16 +02:00
|
|
|
feedback.warnings.push('xsd:', type, 'does not validate for literal. {', triple.subject.value, triple.predicate.value, triple.object.value, '}');
|
2015-07-14 18:16:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback(feedback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
window.validate = validate;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = validate;
|