Breaking Change: The buf::validate::Validator::Validate
method no longer returns a StatusOr
containing a buf::validate::Violations
message, but a StatusOr
containing an instance of the new buf::validate::ValidationResult
class. In most cases, using the proto()
method of the violation is all that needs to be done:
auto result = validator.Validate(message).value();
for (int i = 0; i < result.violations_size(); i++) {
- puts(result.violation(i).GetMessage().c_str())
+ puts(result.violation(i).proto().GetMessage().c_str())
}
buf::validate::ValidationResult
itself also has a proto()
method that returns the protobuf buf::validate::Violations
message equivalent.
The new buf::validate::ConstraintViolation
class provides additional in-memory information about the violation which cannot be serialized to the wire:
field_value()
: Returns anabsl::optional
containing abuf::validate::ProtoField
pointing to the value of the field failing validation, if there is a field corresponding to the violation.rule_value()
: Returns anabsl::optional
containing abuf::validate::ProtoField
pointing to 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
with email
set to "invalid"
, the violation's field_value()->variant()
will contain "invalid"
and the rule_value()->variant()
will contain true
.
Some violations do not correspond directly to a field, such as a message constraint failure; in these cases, the field_value()
will return absl::nullopt
.
What's Changed
- Implement structured field and rule paths for violations by @jchadwick-buf in #63
- Add field and rule value to violations by @jchadwick-buf in #64
Full Changelog: v0.3.0...v0.4.0