Skip to content

Commit

Permalink
Fix ValidationResult#isSuccess() when result is success (#27)
Browse files Browse the repository at this point in the history
Is this just a bug?
expected: violations.isEmpty() => validation is success
current: violations.isEmpty() => validation is **not success**

I think so according to this example.
https://github.com/bufbuild/protovalidate-java#example

---------

Co-authored-by: Philip K. Warren <[email protected]>
Co-authored-by: Philip K. Warren <[email protected]>
  • Loading branch information
3 people authored Sep 11, 2023
1 parent 89e7c88 commit 63d2c4b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import build.buf.validate.conformance.cases.AnEnum;
import build.buf.validate.conformance.cases.BoolConstTrue;
import build.buf.validate.conformance.cases.BytesContains;
import build.buf.validate.conformance.cases.BytesIn;
import build.buf.validate.conformance.cases.DurationGTELTE;
import build.buf.validate.conformance.cases.Embed;
import build.buf.validate.conformance.cases.EnumDefined;
Expand Down Expand Up @@ -64,33 +65,33 @@ public void setUp() {
public void strprefix() throws Exception {
StringPrefix invalid = StringPrefix.newBuilder().setVal("foo").build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations().isEmpty()).isTrue();
assertThat(validate.getViolations()).hasSize(0);
assertThat(validate.getViolations()).isEmpty();
assertThat(validate.isSuccess()).isTrue();
}

@Test
public void bytescontains() throws Exception {
BytesContains invalid =
BytesContains.newBuilder().setVal(ByteString.copyFromUtf8("candy bars")).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations().isEmpty()).isTrue();
assertThat(validate.getViolations()).hasSize(0);
assertThat(validate.getViolations()).isEmpty();
assertThat(validate.isSuccess()).isTrue();
}

@Test
public void strcontains() throws Exception {
StringContains invalid = StringContains.newBuilder().setVal("foobar").build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations().isEmpty()).isTrue();
assertThat(validate.getViolations()).hasSize(0);
assertThat(validate.getViolations()).isEmpty();
assertThat(validate.isSuccess()).isTrue();
}

@Test
public void boolconsttrue() throws Exception {
BoolConstTrue invalid = BoolConstTrue.newBuilder().build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations()).hasSize(1);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
Expand All @@ -99,41 +100,41 @@ public void timestampwithin() throws Exception {
TimestampWithin.newBuilder().setVal(Timestamp.newBuilder().build()).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations()).hasSize(1);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
public void timestampcost() throws Exception {
TimestampConst invalid =
TimestampConst.newBuilder().setVal(Timestamp.newBuilder().setSeconds(3).build()).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations().isEmpty()).isTrue();
assertThat(validate.getViolations()).hasSize(0);
assertThat(validate.getViolations()).isEmpty();
assertThat(validate.isSuccess()).isTrue();
}

@Test
public void OneofIgnoreEmpty() throws Exception {
public void oneofIgnoreEmpty() throws Exception {
OneofIgnoreEmpty invalid =
OneofIgnoreEmpty.newBuilder().setY(ByteString.copyFromUtf8("")).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations().isEmpty()).isTrue();
assertThat(validate.getViolations()).hasSize(0);
assertThat(validate.getViolations()).isEmpty();
assertThat(validate.isSuccess()).isTrue();
}

@Test
public void enumdefined() throws Exception {
EnumDefined invalid = EnumDefined.newBuilder().setValValue(2147483647).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations()).hasSize(1);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
public void strictFixed32LT() throws Exception {
Fixed32LT invalid = Fixed32LT.newBuilder().setVal(5).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations()).hasSize(1);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
Expand All @@ -142,15 +143,15 @@ public void strictWrapperDouble() throws Exception {
WrapperDouble.newBuilder().setVal(DoubleValue.newBuilder().build()).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations()).hasSize(1);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
public void strictFieldExpressions() throws Exception {
FieldExpressions invalid = FieldExpressions.newBuilder().build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations()).hasSize(2);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
Expand All @@ -161,22 +162,22 @@ public void strictDurationGTELTE() throws Exception {
.build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations()).hasSize(1);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
public void strictRepeatedExact() throws Exception {
RepeatedExact invalid = RepeatedExact.newBuilder().addAllVal(Arrays.asList(1, 2)).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
assertThat(validate.getViolations()).hasSize(1);
}

@Test
public void strictSFixed64In() throws Exception {
SFixed64In invalid = SFixed64In.newBuilder().setVal(5).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
assertThat(validate.getViolations()).hasSize(1);
}

Expand All @@ -188,31 +189,31 @@ public void strictFieldExpressionsNested() throws Exception {
.setC(FieldExpressions.Nested.newBuilder().setA(-3).build())
.build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
assertThat(validate.getViolations()).hasSize(4);
}

@Test
public void strictRepeatedExactIgnore() throws Exception {
RepeatedExactIgnore invalid = RepeatedExactIgnore.newBuilder().build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.getViolations().isEmpty()).isTrue();
assertThat(validate.getViolations()).hasSize(0);
assertThat(validate.getViolations()).isEmpty();
assertThat(validate.isSuccess()).isTrue();
}

@Test
public void strictInt32In() throws Exception {
Int32In invalid = Int32In.newBuilder().setVal(4).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
assertThat(validate.getViolations()).hasSize(1);
}

@Test
public void strictRepeatedEnumIn() throws Exception {
RepeatedEnumIn invalid = RepeatedEnumIn.newBuilder().addVal(AnEnum.AN_ENUM_X).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
assertThat(validate.getViolations()).hasSize(1);
}

Expand All @@ -224,7 +225,7 @@ public void strictRepeatedMin() throws Exception {
.addVal(Embed.newBuilder().setVal(-1).build())
.build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
assertThat(validate.getViolations()).hasSize(1);
}

Expand All @@ -234,20 +235,19 @@ public void testDynRuntimeError() throws Exception {
validator.validate(invalid);
}

// Needs : https://github.com/projectnessie/cel-java/pull/419
// @Test
// public void strictBytesIn() throws ValidationException {
// BytesIn invalid = BytesIn.newBuilder().setVal(ByteString.copyFromUtf8("bar")).build();
// ValidationResult validate = validator.validate(invalid);
// assertThat(validate.isSuccess()).isTrue();
// }
@Test
public void strictBytesIn() throws ValidationException {
BytesIn invalid = BytesIn.newBuilder().setVal(ByteString.copyFromUtf8("bar")).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.isSuccess()).isTrue();
}

@Test
public void strictRepeatedUnique() throws ValidationException {
RepeatedUnique invalid =
RepeatedUnique.newBuilder().addAllVal(Arrays.asList("foo", "bar", "foo", "baz")).build();
ValidationResult validate = validator.validate(invalid);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
Expand All @@ -264,14 +264,14 @@ public void testRecursiveInvalid() throws ValidationException {
MapRecursive.newBuilder().putVal(1, MapRecursive.Msg.newBuilder().build()).build();
ValidationResult validate = validator.validate(test);
assertThat(validate.getViolations()).hasSize(1);
assertThat(validate.isSuccess()).isTrue();
assertThat(validate.isSuccess()).isFalse();
}

@Test
public void testStringLenEmoji() throws ValidationException {
StringLen test = StringLen.newBuilder().setVal("😅😄👾").build();
ValidationResult validate = validator.validate(test);
assertThat(validate.getViolations()).hasSize(0);
assertThat(validate.getViolations().isEmpty()).isTrue();
assertThat(validate.getViolations()).isEmpty();
assertThat(validate.isSuccess()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ValidationResult(List<Violation> violations) {
* @return if the validation result was a success.
*/
public boolean isSuccess() {
return !violations.isEmpty();
return violations.isEmpty();
}

/**
Expand Down

0 comments on commit 63d2c4b

Please sign in to comment.