forked from zazuko/rdf-validate-shacl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (71 loc) · 2.76 KB
/
index.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
const clownface = require('clownface')
const { prepareNamespaces } = require('./src/namespaces')
const ShapesGraph = require('./src/shapes-graph')
const ValidationEngine = require('./src/validation-engine')
const shaclVocabularyFactory = require('./src/vocabularies/shacl')
/**
* Validates RDF data based on a set of RDF shapes.
*
* @param {DatasetCore} shapes - Dataset containing the SHACL shapes for validation
* @param {object} options - Validator options
* @param {DataFactory} options.factory - Optional RDFJS data factory
* @param {Number} options.maxErrors - Max number of errors before the engine
* stops. Defaults to finding all the errors.
*/
class SHACLValidator {
constructor (shapes, options) {
options = options || {}
this.factory = options.factory || require('@rdfjs/dataset')
this.ns = prepareNamespaces(this.factory)
this.loadShapes(shapes)
this.validationEngine = new ValidationEngine(this, options)
this.depth = 0
}
/**
* Validates the provided data graph against the provided shapes graph
*
* @param {DatasetCore} data - Dataset containing the data to validate
* @return {ValidationReport} - Result of the validation
*/
validate (data) {
this.$data = clownface({ dataset: data, factory: this.factory })
this.validationEngine.validateAll(this.$data)
return this.validationEngine.getReport()
}
/**
* Validates the provided focus node against the provided shape
*
* @param {DatasetCore} data - Dataset containing the data to validate
* @param {Term} focusNode - Node to validate
* @param {Term} shapeNode - Shape used to validate the node. It must be present in the shapes graph.
* @returns {ValidationReport} - Result of the validation
*/
validateNode (data, focusNode, shapeNode) {
this.$data = clownface({ dataset: data, factory: this.factory })
this.nodeConformsToShape(focusNode, shapeNode)
return this.validationEngine.getReport()
}
/**
* Load SHACL shapes constraints from dataset.
*
* @param {DatasetCore} shapes - Dataset containing the shapes for validation
*/
loadShapes (shapes) {
const shaclQuads = shaclVocabularyFactory(this.factory)
const dataset = this.factory.dataset(shaclQuads.concat([...shapes]))
this.$shapes = clownface({ dataset, factory: this.factory })
this.shapesGraph = new ShapesGraph(this)
}
// Exposed to be available from validation functions as `SHACL.nodeConformsToShape`
nodeConformsToShape (focusNode, shapeNode) {
const shape = this.shapesGraph.getShape(shapeNode)
try {
this.depth++
const foundViolations = this.validationEngine.validateNodeAgainstShape(focusNode, shape, this.$data)
return !foundViolations
} finally {
this.depth--
}
}
}
module.exports = SHACLValidator