Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lists in result messages #129

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-snakes-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rdf-validate-shacl": patch
---

Improved result messages with lists
20 changes: 18 additions & 2 deletions src/validation-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,16 @@ function localName(uri) {
return uri.substring(index + 1)
}

function nodeLabel(node) {
function * take(n, iterable) {
let i = 0
for (const item of iterable) {
if (i++ === n) break
yield item
}
}

function nodeLabel(constraint, param) {
const node = constraint.getParameterValue(param)
if (!node) {
return 'NULL'
}
Expand All @@ -331,6 +340,13 @@ function nodeLabel(node) {
}

if (node.termType === 'BlankNode') {
const pointer = constraint.shapeNodePointer.out(param)
const list = pointer.list()
if (list) {
const items = Array.from(take(3, list))
return items.join(', ') + (items.length === 3 ? ' ...' : '')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be useful to give even more info than just an ellipsis. If it does not affect performance too much

Suggested change
const items = Array.from(take(3, list))
return items.join(', ') + (items.length === 3 ? ' ...' : '')
const { head, count } = take(3, list)
const items = Array.from(head)
return items.join(', ') + (items.length === 3 ? ` (and ${count} more)` : '')

}

return 'Blank node ' + node.value
}

Expand All @@ -340,7 +356,7 @@ function nodeLabel(node) {
function withSubstitutions(messageTerm, constraint, factory) {
const message = constraint.component.parameters.reduce((message, param) => {
const paramName = localName(param.value)
const paramValue = nodeLabel(constraint.getParameterValue(param))
const paramValue = nodeLabel(constraint, param)
return message
.replace(`{$${paramName}}`, paramValue)
.replace(`{?${paramName}}`, paramValue)
Expand Down
2 changes: 1 addition & 1 deletion src/validators-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default {
[ns.sh.InConstraintComponent.value]: {
validator: {
func: validators.validateIn,
message: 'Value is not in {$in}',
message: 'Value is not one of the allowed values: {$in}',
},
},
[ns.sh.LanguageInConstraintComponent.value]: {
Expand Down
11 changes: 11 additions & 0 deletions test/data/validation-message/message-with-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 xsd: <http://www.w3.org/2001/XMLSchema#> .

<Alice> a <Person> ; <category> "f" .

<ShapeWithList> a sh:NodeShape ;
sh:targetClass <Person> ;
sh:property [
sh:path <category> ;
sh:in ("a" "b" "c" "d" "e") ;
] .
13 changes: 13 additions & 0 deletions test/validation_message_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@ describe('validation messages', () => {
{ value: 'Mon message de validation', language: 'fr' },
]))
})

it('Lists first items in message', async () => {
const dataPath = path.join(rootPath, 'message-with-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)
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 ...')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the message look like when items are IRIs?

})
})
Loading