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

fixes https://github.com/zazuko/rdf-validate-shacl/issues/136 #137

Merged
merged 13 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SHACLValidator {
this.allowNamedNodeInList = options.allowNamedNodeInList === undefined ? false : options.allowNamedNodeInList
this.loadShapes(shapes)
this.validationEngine = new ValidationEngine(this, options)
this.checkedNodes = new Set()

this.depth = 0
}
Expand All @@ -34,6 +35,7 @@ class SHACLValidator {
* @return {ValidationReport} - Result of the validation
*/
validate(data) {
this.checkedNodes.clear()
this.$data = clownface({ dataset: data, factory: this.factory })
this.validationEngine.validateAll(this.$data)
return this.validationEngine.getReport()
Expand All @@ -48,6 +50,7 @@ class SHACLValidator {
* @returns {ValidationReport} - Result of the validation
*/
validateNode(data, focusNode, shapeNode) {
this.checkedNodes.clear()
this.$data = clownface({ dataset: data, factory: this.factory })
this.nodeConformsToShape(focusNode, shapeNode, this.validationEngine)
return this.validationEngine.getReport()
Expand All @@ -67,7 +70,10 @@ class SHACLValidator {
}

// Exposed to be available from validation functions as `SHACL.nodeConformsToShape`
nodeConformsToShape(focusNode, shapeNode, engine = this.validationEngine.clone()) {
nodeConformsToShape(focusNode, shapeNode, engine = this.validationEngine.clone(), resetCheckedNodes = false) {
s-tittel marked this conversation as resolved.
Show resolved Hide resolved
if (resetCheckedNodes) {
this.checkedNodes.clear()
}
const shape = this.shapesGraph.getShape(shapeNode)
try {
this.depth++
Expand Down
8 changes: 8 additions & 0 deletions src/validation-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class ValidationEngine {

if (shape.deactivated) return false

// have we already checked this focusNode against the shape? If so, don't check again to prevent possible recursion.
let id = JSON.stringify([focusNode, shape.shapeNode])
if (this.context.checkedNodes.has(id)) {
return false
}
// mark given focusNode/shape pair as 'checked'.
this.context.checkedNodes.add(id)

const valueNodes = shape.getValueNodes(focusNode, dataGraph)
let errorFound = false
for (const constraint of shape.constraints) {
Expand Down
2 changes: 1 addition & 1 deletion src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function validateQualifiedHelper(context, focusNode, constraint) {
const path = constraint.shape.pathObject
return getPathObjects(context.$data, focusNode, path)
.filter(value =>
context.nodeConformsToShape(value, qualifiedValueShapeNode) &&
context.nodeConformsToShape(value, qualifiedValueShapeNode, context.validationEngine, true) &&
!validateQualifiedConformsToASibling(context, value, [...siblingShapes]),
)
.length
Expand Down
12 changes: 6 additions & 6 deletions test/data/data-shapes/core/node/xone-duplicate.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
sht:shapesGraph <xone-duplicate-shapes.ttl> ] ;
mf:result [ rdf:type sh:ValidationReport ;
sh:conforms "false"^^xsd:boolean ;
sh:result [ rdf:type sh:ValidationResult ;
sh:resultSeverity sh:Violation ;
sh:focusNode ex:i ;
sh:value ex:i ;
sh:sourceShape ex:s1 ;
sh:sourceConstraintComponent sh:XoneConstraintComponent ] ;
sh:result [ rdf:type sh:ValidationResult ;
sh:resultSeverity sh:Violation ;
sh:focusNode ex:j ;
sh:value ex:j ;
sh:sourceShape ex:s1 ;
sh:sourceConstraintComponent sh:XoneConstraintComponent ] ] ;
sh:result [ rdf:type sh:ValidationResult ;
sh:resultSeverity sh:Violation ;
sh:focusNode ex:i ;
sh:value ex:i ;
sh:sourceShape ex:s1 ;
sh:sourceConstraintComponent sh:XoneConstraintComponent ] ;
tpluscode marked this conversation as resolved.
Show resolved Hide resolved
mf:status sht:approved .
9 changes: 0 additions & 9 deletions test/data/data-shapes/core/property/property-001.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ ex:ValidPerson1
sh:sourceShape ex:PersonShape-address-city ;
sh:value ex:InvalidCity ;
] ;
sh:result [
rdf:type sh:ValidationResult ;
sh:focusNode ex:InvalidAddress ;
sh:resultPath ex:city ;
sh:resultSeverity sh:Violation ;
sh:sourceConstraintComponent sh:ClassConstraintComponent ;
sh:sourceShape ex:PersonShape-address-city ;
sh:value ex:InvalidCity ;
] ;
tpluscode marked this conversation as resolved.
Show resolved Hide resolved
] ;
mf:status sht:approved ;
.
9 changes: 0 additions & 9 deletions test/data/data-shapes/core/validation-reports/shared.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@
mf:result [
rdf:type sh:ValidationReport ;
sh:conforms "false"^^xsd:boolean ;
sh:result [
rdf:type sh:ValidationResult ;
sh:resultSeverity sh:Violation ;
sh:focusNode ex:j ;
sh:value ex:k ;
sh:resultPath ex:r ;
sh:sourceShape ex:s4 ;
sh:sourceConstraintComponent sh:ClassConstraintComponent
] ;
sh:result [
rdf:type sh:ValidationResult ;
sh:resultSeverity sh:Violation ;
Expand Down
48 changes: 48 additions & 0 deletions test/data/data-shapes/custom/circularReferences.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@prefix dash: <http://datashapes.org/dash#> .
@prefix ex: <http://example.org#> .
@prefix mf: <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix sht: <http://www.w3.org/ns/shacl-test#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<>
rdf:type mf:Manifest ;
mf:entries (
<circularReferences>
) ;
.

<circularReferences>
rdf:type sht:Validate ;
rdfs:label "Test of circular references in data graph" ;
mf:action [
sht:dataGraph <> ;
sht:shapesGraph <> ;
] ;
mf:result [
rdf:type sh:ValidationReport ;
sh:conforms "true"^^xsd:boolean ;
] ;
.

ex:Thing
a sh:NodeShape,rdfs:Class ;
sh:property [
sh:path ex:references ;
sh:node ex:Thing
] ;
sh:targetClass <Thing>
.

<thing1>
a <Thing> ;
ex:references <thing2>
.

<thing2>
a <Thing> ;
ex:references <thing1> ;
.
1 change: 1 addition & 0 deletions test/data/data-shapes/custom/manifest.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
mf:include <multiLevelInheritanceWithImplicitTarget.ttl> ;
mf:include <targetNodeDoesNotExist.ttl> ;
mf:include <lessThan-moreTypes.ttl> ;
mf:include <circularReferences.ttl> ;
.