-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.js
90 lines (73 loc) · 2.47 KB
/
Validator.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
87
88
89
90
import TermMap from '@rdfjs/term-map'
import { PathList } from 'grapoi'
import Context from './lib/Context.js'
import * as ns from './lib/namespaces.js'
import Registry from './lib/Registry.js'
import Shape from './lib/Shape.js'
import validations from './lib/validations.js'
class Validator {
constructor (dataset, { factory, ...options }) {
this.factory = factory
this.options = options
this.registry = new Registry(validations)
this.shapesPtr = new PathList({ dataset, factory })
this.shapes = new TermMap()
if (this.options.coverage) {
this.options.debug = true
this.options.details = true
this.options.trace = true
}
if (this.options.validations) {
for (const [key, value] of this.options.validations) {
this.registry.validations.set(key, value)
}
}
const shapePtrs = [
...this.shapesPtr.hasOut([ns.sh.targetClass]),
...this.shapesPtr.hasOut([ns.sh.targetNode]),
...this.shapesPtr.hasOut([ns.sh.targetObjectsOf]),
...this.shapesPtr.hasOut([ns.sh.targetSubjectsOf]),
...this.shapesPtr.hasOut([ns.rdf.type], [ns.sh.NodeShape]),
...this.shapesPtr.hasOut([ns.rdf.type], [ns.sh.PropertyShape])
]
for (const shapePtr of shapePtrs) {
this.shape(shapePtr)
}
}
shape (ptr) {
if (!ptr.term) {
return null
}
let shape = this.shapes.get(ptr.term)
if (!shape) {
shape = new Shape(ptr, { validator: this })
this.shapes.set(ptr.term, shape)
}
return shape
}
async validate (data, shapes) {
const focusNode = new PathList({ ...data, factory: this.factory })
const context = new Context({ factory: this.factory, focusNode, options: this.options, validator: this })
if (shapes) {
// if shapes are given, use only the term for the lookup in the ptr from the constructor
shapes = shapes.map(shape => this.shape(this.shapesPtr.node(shape.terms)))
} else {
shapes = this.shapes.values()
}
for (const shape of shapes) {
const shapeContext = context.create({ shape })
let targets
// if the focusNode has already terms (given as argument), there is no need to resolve the targets
if (!focusNode.isAny()) {
targets = focusNode
} else {
targets = shape.resolveTargets(shapeContext)
}
for (const focusNode of targets) {
await shape.validate(shapeContext.create({ focusNode }))
}
}
return context.report
}
}
export default Validator