Skip to content

v0.5.0

Latest
Compare
Choose a tag to compare
@jchadwick-buf jchadwick-buf released this 12 Dec 20:39
· 19 commits to main since this release
df11bf2

Breaking Change: The build.buf.protovalidate.ValidationResult.getViolations() method no longer returns a list of protobuf build.buf.validate.Violations messages, but a list of a new wrapper class, build.buf.protovalidate.Violation. In most cases, changing the import for Violation and calling the toProto() method of the violation is all that needs to be done:

     // Note that the import should also change from `build.buf.validate.Violation`
     // to `build.buf.protovalidate.Violation`.
     for (Violation violation : validator.validate(msg).getViolations()) {
-        System.out.println(violation.getConstraintId());
+        System.out.println(violation.toProto().getConstraintId());
     }

ValidationResult also has a toProto() method that returns the protobuf build.buf.validate.Violations message equivalent. This can be used to get a list of protobuf build.buf.validate.Violation messages, using the getViolationsList() getter of said message, as before.

+    List<build.buf.validate.Violation> violations = validator.validate(msg).getViolations();
-    List<build.buf.validate.Violation> violations = validator.validate(msg).toProto().getViolationsList();

The new build.buf.protovalidate.Violation class provides additional getters for in-memory information about the violation which cannot be serialized to the wire:

  • getFieldValue(): Returns the value of the field failing validation, if there is a field corresponding to the violation.
  • getRuleValue(): Returns the value of the rule failing validation, if there is a rule corresponding to the violation.

Take, for example, the following protobuf message schema:

message User {
    string email = 1 [(buf.validate.field).string.email = true];
}

If you try to validate the message User.newBuilder().setEmail("invalid").build(), getFieldValue().getValue() will return "invalid" and getRuleValue().getValue() will return true.

Some violations do not correspond directly to a field, such as a message constraint failure; in these cases, the return value of getFieldValue() will be null.

What's Changed

Full Changelog: v0.4.2...v0.5.0