-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
86 lines (76 loc) · 3.06 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
81
82
83
84
85
86
import clownface from 'clownface'
import shaclVocabularyFactory from '@vocabulary/sh'
import factory from './src/defaultEnv.js'
import { prepareNamespaces } from './src/namespaces.js'
import ShapesGraph from './src/shapes-graph.js'
import ValidationEngine from './src/validation-engine.js'
/**
* 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 || factory
this.ns = prepareNamespaces(this.factory)
this.allowNamedNodeInList = options.allowNamedNodeInList === undefined ? false : options.allowNamedNodeInList
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, this.validationEngine)
return this.validationEngine.getReport()
}
/**
* Load SHACL shapes constraints from dataset.
*
* @param {DatasetCore} shapes - Dataset containing the shapes for validation
*/
loadShapes(shapes) {
const shaclQuads = shaclVocabularyFactory({ factory: 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, engine = this.validationEngine.clone()) {
const shape = this.shapesGraph.getShape(shapeNode)
try {
this.depth++
const foundViolations = engine.validateNodeAgainstShape(focusNode, shape, this.$data)
return !foundViolations
} finally {
this.depth--
}
}
validateNodeAgainstShape (focusNode, shapeNode) {
return this.nodeConformsToShape(focusNode, shapeNode, this.validationEngine)
}
}
export default SHACLValidator