Skip to content

Commit e520be3

Browse files
fkorotkovandimarek
authored andcommitted
Include field name in error description
1 parent ca04087 commit e520be3

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/main/java/graphql/validation/ValidationError.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public String getMessage() {
4242
return String.format("Validation error of type %s: %s", validationErrorType, description);
4343
}
4444

45+
public String getDescription() {
46+
return description;
47+
}
48+
4549
@Override
4650
public List<SourceLocation> getLocations() {
4751
return sourceLocations;

src/main/java/graphql/validation/ValidationErrorCollector.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,22 @@ public List<ValidationError> getErrors() {
2020
}
2121

2222
public boolean containsValidationError(ValidationErrorType validationErrorType) {
23+
return containsValidationError(validationErrorType, null);
24+
}
25+
26+
public boolean containsValidationError(ValidationErrorType validationErrorType, String description) {
2327
for (ValidationError validationError : errors) {
24-
if (validationError.getValidationErrorType() == validationErrorType) return true;
28+
if (validationError.getValidationErrorType() == validationErrorType) {
29+
return description == null || validationError.getDescription().equals(description);
30+
}
2531
}
2632
return false;
2733
}
34+
35+
@Override
36+
public String toString() {
37+
return "ValidationErrorCollector{" +
38+
"errors=" + errors +
39+
'}';
40+
}
2841
}

src/main/java/graphql/validation/rules/ScalarLeafs.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public void checkField(Field field) {
2020
if (type == null) return;
2121
if (schemaUtil.isLeafType(type)) {
2222
if (field.getSelectionSet() != null) {
23-
String message = String.format("Sub selection not allowed on leaf type %s", type.getName());
23+
String message = String.format("Sub selection not allowed on leaf type %s of field %s", type.getName(), field.getName());
2424
addError(new ValidationError(ValidationErrorType.SubSelectionNotAllowed, field.getSourceLocation(), message));
2525
}
2626
} else {
2727
if (field.getSelectionSet() == null) {
28-
String message = String.format("Sub selection required for type %s", type.getName());
28+
String message = String.format("Sub selection required for type %s of field %s", type.getName(), field.getName());
2929
addError(new ValidationError(ValidationErrorType.SubSelectionRequired, field.getSourceLocation(), message));
3030
}
3131
}

src/test/groovy/graphql/validation/rules/ScalarLeafsTest.groovy

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class ScalarLeafsTest extends Specification {
2323
scalarLeafs.checkField(field)
2424

2525
then:
26-
errorCollector.containsValidationError(ValidationErrorType.SubSelectionNotAllowed)
26+
errorCollector.containsValidationError(
27+
ValidationErrorType.SubSelectionNotAllowed,
28+
"Sub selection not allowed on leaf type String of field hello"
29+
)
2730
}
2831

2932
def "sub selection required"() {
@@ -34,6 +37,9 @@ class ScalarLeafsTest extends Specification {
3437
scalarLeafs.checkField(field)
3538

3639
then:
37-
errorCollector.containsValidationError(ValidationErrorType.SubSelectionRequired)
40+
errorCollector.containsValidationError(
41+
ValidationErrorType.SubSelectionRequired,
42+
"Sub selection required for type objectType of field hello"
43+
)
3844
}
3945
}

0 commit comments

Comments
 (0)