Skip to content

Commit 45271ab

Browse files
authored
Fix operator in ValidationResult toString (#286)
Fixes #285 This fixes the check for field path elements when calculating the `toString` value for `ValidationResult` and adds a few tests. Note that this also changes the value to print `Validation OK` if there are no violations since it didn't make much sense to print `Validation Error:` if there are no violations. Technically this is a breaking change.
1 parent fd4472a commit 45271ab

File tree

2 files changed

+136
-9
lines changed

2 files changed

+136
-9
lines changed

src/main/java/build/buf/protovalidate/ValidationResult.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,20 @@ public List<Violation> getViolations() {
6868
@Override
6969
public String toString() {
7070
StringBuilder builder = new StringBuilder();
71-
builder.append("Validation error:");
72-
for (Violation violation : violations) {
73-
builder.append("\n - ");
74-
if (!violation.toProto().hasField()) {
75-
builder.append(FieldPathUtils.fieldPathString(violation.toProto().getField()));
76-
builder.append(": ");
71+
if (isSuccess()) {
72+
builder.append("Validation OK");
73+
} else {
74+
builder.append("Validation error:");
75+
for (Violation violation : violations) {
76+
builder.append("\n - ");
77+
if (violation.toProto().hasField()) {
78+
builder.append(FieldPathUtils.fieldPathString(violation.toProto().getField()));
79+
builder.append(": ");
80+
}
81+
builder.append(
82+
String.format(
83+
"%s [%s]", violation.toProto().getMessage(), violation.toProto().getRuleId()));
7784
}
78-
builder.append(
79-
String.format(
80-
"%s [%s]", violation.toProto().getMessage(), violation.toProto().getRuleId()));
8185
}
8286
return builder.toString();
8387
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2023-2024 Buf Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package build.buf.protovalidate;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
import build.buf.validate.FieldPathElement;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import org.junit.jupiter.api.Test;
23+
24+
class ValidationResultTest {
25+
@Test
26+
void testToStringNoViolations() {
27+
28+
List<Violation> violations = new ArrayList<Violation>();
29+
ValidationResult result = new ValidationResult(violations);
30+
31+
assertThat(result.toString()).isEqualTo("Validation OK");
32+
assertThat(result.isSuccess()).isTrue();
33+
}
34+
35+
@Test
36+
void testToStringSingleViolation() {
37+
FieldPathElement elem =
38+
FieldPathElement.newBuilder().setFieldNumber(5).setFieldName("test_field_name").build();
39+
40+
RuleViolation violation =
41+
RuleViolation.newBuilder()
42+
.setRuleId("int32.const")
43+
.setMessage("value must equal 42")
44+
.addFirstFieldPathElement(elem)
45+
.build();
46+
List<Violation> violations = new ArrayList<Violation>();
47+
violations.add(violation);
48+
ValidationResult result = new ValidationResult(violations);
49+
50+
assertThat(result.toString())
51+
.isEqualTo("Validation error:\n - test_field_name: value must equal 42 [int32.const]");
52+
}
53+
54+
@Test
55+
void testToStringMultipleViolations() {
56+
FieldPathElement elem =
57+
FieldPathElement.newBuilder().setFieldNumber(5).setFieldName("test_field_name").build();
58+
59+
RuleViolation violation1 =
60+
RuleViolation.newBuilder()
61+
.setRuleId("int32.const")
62+
.setMessage("value must equal 42")
63+
.addFirstFieldPathElement(elem)
64+
.build();
65+
66+
RuleViolation violation2 =
67+
RuleViolation.newBuilder()
68+
.setRuleId("int32.required")
69+
.setMessage("value is required")
70+
.addFirstFieldPathElement(elem)
71+
.build();
72+
List<Violation> violations = new ArrayList<Violation>();
73+
violations.add(violation1);
74+
violations.add(violation2);
75+
ValidationResult result = new ValidationResult(violations);
76+
77+
assertThat(result.toString())
78+
.isEqualTo(
79+
"Validation error:\n - test_field_name: value must equal 42 [int32.const]\n - test_field_name: value is required [int32.required]");
80+
}
81+
82+
@Test
83+
void testToStringSingleViolationMultipleFieldPathElements() {
84+
FieldPathElement elem1 =
85+
FieldPathElement.newBuilder().setFieldNumber(5).setFieldName("test_field_name").build();
86+
FieldPathElement elem2 =
87+
FieldPathElement.newBuilder().setFieldNumber(5).setFieldName("nested_name").build();
88+
89+
List<FieldPathElement> elems = new ArrayList<FieldPathElement>();
90+
elems.add(elem1);
91+
elems.add(elem2);
92+
93+
RuleViolation violation1 =
94+
RuleViolation.newBuilder()
95+
.setRuleId("int32.const")
96+
.setMessage("value must equal 42")
97+
.addAllFieldPathElements(elems)
98+
.build();
99+
100+
List<Violation> violations = new ArrayList<Violation>();
101+
violations.add(violation1);
102+
ValidationResult result = new ValidationResult(violations);
103+
104+
assertThat(result.toString())
105+
.isEqualTo(
106+
"Validation error:\n - test_field_name.nested_name: value must equal 42 [int32.const]");
107+
}
108+
109+
@Test
110+
void testToStringSingleViolationNoFieldPathElements() {
111+
RuleViolation violation =
112+
RuleViolation.newBuilder()
113+
.setRuleId("int32.const")
114+
.setMessage("value must equal 42")
115+
.build();
116+
List<Violation> violations = new ArrayList<Violation>();
117+
violations.add(violation);
118+
ValidationResult result = new ValidationResult(violations);
119+
120+
assertThat(result.toString())
121+
.isEqualTo("Validation error:\n - value must equal 42 [int32.const]");
122+
}
123+
}

0 commit comments

Comments
 (0)