Skip to content

Commit 2646af7

Browse files
committed
Improve validation messages for sh:closed and sh:xone
1 parent 94a925d commit 2646af7

30 files changed

+81
-44
lines changed

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/ClassTypeViolation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public String errorCode() {
2424
}
2525

2626
@Override
27-
public String message() {
27+
public String violationSpecificMessage() {
2828
return String.format( "Property %s on %s has type %s, but only %s is allowed.",
2929
propertyName(), elementName(), shortUri( actualClass().getURI() ), shortUri( allowedClass().getURI() ) );
3030
}

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/ClosedViolation.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,22 @@ public String errorCode() {
2929
}
3030

3131
@Override
32-
public String message() {
33-
final Set<String> allowed = Stream.concat( allowedProperties().stream(), ignoredProperties().stream() )
32+
public String violationSpecificMessage() {
33+
final List<String> allowed = allowedProperties().stream()
3434
.map( Property::getURI )
3535
.map( this::shortUri )
36-
.collect( Collectors.toSet() );
37-
return String.format( "%s is used on %s. It is not allowed there; allowed are only %s.",
38-
shortUri( actual.getURI() ), elementName(), allowed );
36+
.collect( Collectors.toSet() )
37+
.stream()
38+
.sorted()
39+
.toList();
40+
final String allowedText = switch ( allowed.size() ) {
41+
case 0 -> "no properties are allowed";
42+
case 1 -> "only " + allowed.iterator().next() + " is allowed";
43+
default -> "allowed are only " + allowed;
44+
};
45+
46+
return String.format( "%s is used on %s. It is not allowed there; %s.",
47+
shortUri( actual.getURI() ), elementName(), allowedText );
3948
}
4049

4150
@Override

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/DatatypeViolation.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ public String errorCode() {
3636
}
3737

3838
@Override
39-
public String message() {
39+
public String violationSpecificMessage() {
4040
if ( context.property().isPresent() ) {
41-
return allowedTypeUri.equals( RDF.langString.getURI() ) && actualTypeUri.equals( XSD.xstring.getURI() ) ?
42-
String.format( "Property %s on %s is missing a language tag.",
43-
propertyName(), elementName() ) :
44-
String.format( "Property %s on %s uses data type %s, but only %s is allowed.",
45-
propertyName(), elementName(), shortUri( actualTypeUri ), shortUri( allowedTypeUri ) );
41+
if ( allowedTypeUri.equals( RDF.langString.getURI() ) && actualTypeUri.equals( XSD.xstring.getURI() ) ) {
42+
return String.format( "Property %s on %s is missing a language tag.", propertyName(), elementName() );
43+
} else if ( allowedTypeUri.equals( XSD.xstring.getURI() ) && actualTypeUri.equals( RDF.langString.getURI() ) ) {
44+
return String.format( "Property %s on %s must not have a language tag.", propertyName(), elementName() );
45+
} else {
46+
return String.format( "Property %s on %s uses data type %s, but only %s is allowed.",
47+
propertyName(), elementName(), shortUri( actualTypeUri ), shortUri( allowedTypeUri ) );
48+
}
4649
}
4750
return String.format( "%s uses data type %s, but only %s is allowed.",
4851
elementName(), shortUri( actualTypeUri ), shortUri( allowedTypeUri ) );

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/DisjointViolation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public String errorCode() {
2525
}
2626

2727
@Override
28-
public String message() {
28+
public String violationSpecificMessage() {
2929
return String.format( "Property %s on %s may not have the same value as property %s (%s).",
3030
propertyName(), elementName(), shortUri( otherProperty.getURI() ), otherValue );
3131
}

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/EqualsViolation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public String errorCode() {
2626
}
2727

2828
@Override
29-
public String message() {
29+
public String violationSpecificMessage() {
3030
return String.format( "Property %s on %s must have the same value as property %s (%s), but has value %s.",
3131
propertyName(), elementName(), shortUri( otherProperty.getURI() ), allowedValue, actualValue );
3232
}

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/EvaluationContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
public record EvaluationContext( Resource element, Shape shape, Optional<Shape.Property> propertyShape, Optional<Property> property,
2828
List<Statement> offendingStatements, ShaclValidator validator, Model resolvedModel ) {
2929
public EvaluationContext withProperty( final Property newProperty ) {
30-
return new EvaluationContext( element(), shape(), Optional.of( newProperty ), offendingStatements(), validator(), resolvedModel );
30+
return new EvaluationContext( element(), shape(), propertyShape(), Optional.of( newProperty ), offendingStatements(), validator(),
31+
resolvedModel );
3132
}
3233

3334
public EvaluationContext withElement( final Resource newElement ) {
34-
return new EvaluationContext( newElement, shape(), property(), offendingStatements(), validator(), resolvedModel );
35+
return new EvaluationContext( newElement, shape(), propertyShape(), property(), offendingStatements(), validator(), resolvedModel );
3536
}
3637

3738
public EvaluationContext withOffendingStatements( final List<Statement> newOffendingStatements ) {
38-
return new EvaluationContext( element(), shape(), property(), newOffendingStatements, validator(), resolvedModel );
39+
return new EvaluationContext( element(), shape(), propertyShape(), property(), newOffendingStatements, validator(), resolvedModel );
3940
}
4041
}

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/InvalidSyntaxViolation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public EvaluationContext context() {
3232
return null;
3333
}
3434

35+
@Override
36+
public String message() {
37+
return violationSpecificMessage;
38+
}
39+
3540
@Override
3641
public <T> T accept( final Visitor<T> visitor ) {
3742
return visitor.visitInvalidSyntaxViolation( this );

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/InvalidValueViolation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public String errorCode() {
2424
}
2525

2626
@Override
27-
public String message() {
27+
public String violationSpecificMessage() {
2828
return String.format( "Property %s on %s has value %s, but only %s is allowed.",
2929
propertyName(), elementName(), value( actual ), value( allowed ) );
3030
}

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/JsConstraintViolation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public String errorCode() {
3737
}
3838

3939
@Override
40-
public String message() {
40+
public String violationSpecificMessage() {
4141
if ( constraintMessage().isEmpty() ) {
4242
return context.property().isPresent() ?
4343
String.format( "Property %s on %s is invalid.", propertyName(), elementName() ) :
4444
String.format( "%s is invalid.", elementName() );
4545
}
4646
String interpolatedMessage = bindings.getOrDefault( "message", constraintMessage() ).toString();
4747
for ( final Map.Entry<String, Object> entry : bindings.entrySet() ) {
48-
String value = "";
48+
final String value;
4949
if ( entry.getValue() instanceof final Node_Literal literal ) {
5050
value = literal.getLiteralLexicalForm();
5151
} else if ( entry.getValue() instanceof final Node_URI namedNode ) {

core/esmf-aspect-model-validator/src/main/java/org/eclipse/esmf/aspectmodel/shacl/violation/LanguageFromListViolation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public String errorCode() {
2424
}
2525

2626
@Override
27-
public String message() {
27+
public String violationSpecificMessage() {
2828
return String.format( "Property %s on %s has language tag %s, which is not in the list of allowed languages: %s.",
2929
propertyName(), elementName(), actual, allowed );
3030
}

0 commit comments

Comments
 (0)