Skip to content

Commit

Permalink
message for sh:xone
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomociti committed Jun 25, 2024
1 parent e865cac commit 5ffdc02
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,22 @@ function validateXone(context, focusNode, valueNode, constraint) {
const { sh } = context.ns
const xoneNode = constraint.getParameterValue(sh.xone)
const shapes = rdfListToArray(context.$shapes.node(xoneNode))
const conformsCount = shapes
.map(shape => context.nodeConformsToShape(valueNode, shape))
.filter(Boolean)
.length

return conformsCount === 1
const conformsPositions = []
for (let i = 0; i < shapes.length; i++) {
const shape = shapes[i]
if (context.nodeConformsToShape(valueNode, shape)) {
conformsPositions.push(i + 1)
}
}

if (conformsPositions.length === 0) {
return ['no shapes matched']
}
if (conformsPositions.length > 1) {
return ['multiple matches, at positions ' + conformsPositions.join(', ')]
}
return []
}

// Private helper functions
Expand Down
12 changes: 12 additions & 0 deletions test/data/validation-message/message-for-xone.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@prefix sh: <http://www.w3.org/ns/shacl#> .

<Alice> a <Person> , <Category1> , <Category3> .

<ShapeWithXone> a sh:NodeShape ;
sh:targetClass <Person> ;
sh:xone (
[ sh:class <Category1> ]
[ sh:class <Category2> ]
[ sh:class <Category3> ]
) ;
.
13 changes: 13 additions & 0 deletions test/validation_message_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,17 @@ describe('validation messages', () => {
assert.strictEqual(report.results[0].message.length, 1)
assert.strictEqual(report.results[0].message[0].value, 'Value is not one of the allowed values: a, b, c ... (and 2 more)')
})

it('Lists conforming shapes in message for xone', async () => {
const dataPath = path.join(rootPath, 'message-for-xone.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)
assert.strictEqual(report.results[0].message.length, 1)
assert.strictEqual(report.results[0].message[0].value, 'multiple matches, at positions 1, 3')
})
})

0 comments on commit 5ffdc02

Please sign in to comment.