Skip to content

Commit

Permalink
fix: ensure nodeList availability
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomociti committed May 6, 2024
1 parent 9999751 commit f732f93
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/shapes-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import NodeSet from './node-set.js'
import ValidationFunction from './validation-function.js'
import validatorsRegistry from './validators-registry.js'
import { extractPropertyPath, getPathObjects } from './property-path.js'
import { getInstancesOf, isInstanceOf } from './dataset-utils.js'
import { getInstancesOf, isInstanceOf, rdfListToArray } from './dataset-utils.js'

class ShapesGraph {
constructor(context) {
Expand Down Expand Up @@ -124,6 +124,14 @@ class Constraint {
get componentMessages() {
return this.component.getMessages(this.shape)
}

get nodeSet() {
const { sh } = this.shape.context.ns
if (!this.inNodeSet) {
this.inNodeSet = new NodeSet(rdfListToArray(this.shapeNodePointer.out(sh.in)))
}
return this.inNodeSet
}
}

class ConstraintComponent {
Expand Down
9 changes: 1 addition & 8 deletions src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,7 @@ function validateHasValueProperty(context, focusNode, valueNode, constraint) {
}

function validateIn(context, focusNode, valueNode, constraint) {
const { sh } = context.ns
if (!constraint.nodeSet) {
const inNode = constraint.getParameterValue(sh.in)
constraint.nodeSet = new NodeSet(rdfListToArray(context.$shapes.node(inNode)))
}
const { nodeSet } = constraint

return nodeSet.has(valueNode)
return constraint.nodeSet.has(valueNode)
}

function validateLanguageIn(context, focusNode, valueNode, constraint) {
Expand Down
11 changes: 11 additions & 0 deletions test/data/validation-sourceShape/mandatory-list.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix ex: <http://example.com/> .

ex:p1 a ex:Person .

ex:shape sh:targetClass ex:Person ;
sh:property [
sh:path ex:category ;
sh:minCount 1 ;
sh:in (1 2 3) ;
] .
19 changes: 19 additions & 0 deletions test/validation_sourceShape_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import path from 'path'
import assert from 'assert'
import * as url from 'url'
import clownface from 'clownface'
import SHACLValidator from '../index.js'
import ns from '../src/namespaces.js'
import { rdfListToArray } from '../src/dataset-utils.js'
import { loadDataset } from './utils.js'

const { rdfs, sh } = ns
Expand All @@ -30,4 +32,21 @@ describe('validation source shapes', () => {
const [comment] = report.dataset.match(shape, rdfs.comment, null)
assert.strictEqual(comment.object.value, 'sh:in has 5 elements and has been removed from the report for brevity. Please refer the original shape')
})
it('Includes source shape with list', async () => {
const dataPath = path.join(rootPath, 'mandatory-list.ttl')
const data = await loadDataset(dataPath)
const shapes = data

const validator = new SHACLValidator(shapes)
const report = validator.validate(data)

assert.strictEqual(report.results.length, 1)
const sourceShapes = new Set(report.results.map(result => result.sourceShape))
assert.strictEqual(sourceShapes.size, 1)
const [shape] = sourceShapes
// the shape has the lst of allowed values even though the violated constraint is sh:minCount and not sh:in
const list = clownface({ dataset: report.dataset, term: shape }).out(sh.in)
const actual = rdfListToArray(list).map(term => term.value)
assert.deepEqual(actual, ['1', '2', '3'])
})
})

0 comments on commit f732f93

Please sign in to comment.