Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ bazel_dep(name = "abseil-cpp", version = "20240722.0", repo_name = "com_google_a

bazel_dep(name = "cel-cpp", version = "0.11.0", repo_name = "com_google_cel_cpp")

bazel_dep(name = "protovalidate", version = "1.0.0-rc.1", repo_name = "com_github_bufbuild_protovalidate")
bazel_dep(name = "protovalidate", version = "1.0.0-rc.2", repo_name = "com_github_bufbuild_protovalidate")
6 changes: 3 additions & 3 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions buf/validate/internal/message_rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ Rules NewMessageRules(
return rules_or.status();
}
result.emplace_back(std::move(rules_or).value());

// buf.validate.MessageRules.oneof
for (const auto& msgOneof : msgLvl.oneof()) {
std::vector<const google::protobuf::FieldDescriptor *> fields;
for (const auto& name : msgOneof.fields()) {
auto fdesc = descriptor->FindFieldByName(name);
if (fdesc == nullptr) {
return absl::FailedPreconditionError(absl::StrCat("field \"", name, "\" not found in message ", descriptor->full_name()));
}
fields.push_back(fdesc);
}
result.emplace_back(std::make_unique<MessageOneofValidationRules>(fields, msgOneof.required()));
}
}

for (int i = 0; i < descriptor->field_count(); i++) {
Expand Down
29 changes: 29 additions & 0 deletions buf/validate/internal/rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,33 @@ absl::Status OneofValidationRules::Validate(
return absl::OkStatus();
}

absl::Status MessageOneofValidationRules::Validate(
RuleContext& ctx, const google::protobuf::Message& message) const {
int has_count = 0;
for (const auto& fdesc : fields_) {
if (message.GetReflection()->HasField(message, fdesc)) {
has_count++;
}
}
if (has_count > 1) {
Violation violation;
*violation.mutable_rule_id() = "message.oneof";
*violation.mutable_message() = absl::StrCat("only one of ", field_names_(), " can be set");
ctx.violations.emplace_back(std::move(violation), absl::nullopt, absl::nullopt);
}
if (required_ && has_count == 0) {
Violation violation;
*violation.mutable_rule_id() = "message.oneof";
*violation.mutable_message() = absl::StrCat("one of ", field_names_(), " must be set");
ctx.violations.emplace_back(std::move(violation), absl::nullopt, absl::nullopt);
}
return absl::OkStatus();
}

std::string MessageOneofValidationRules::field_names_() const {
return absl::StrJoin(fields_, ", ", [](std::string* out, const google::protobuf::FieldDescriptor *fdesc) {
absl::StrAppend(out, fdesc->name());
});
}

} // namespace buf::validate::internal
15 changes: 15 additions & 0 deletions buf/validate/internal/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ class OneofValidationRules : public ValidationRules {
bool required_ = false;
};

class MessageOneofValidationRules : public ValidationRules {
using Base = ValidationRules;

public:
MessageOneofValidationRules(const std::vector<const google::protobuf::FieldDescriptor*> fields, const bool required)
: fields_(fields), required_(required) {}

absl::Status Validate(RuleContext& ctx, const google::protobuf::Message& message) const override;

private:
const std::vector<const google::protobuf::FieldDescriptor*> fields_;
bool required_ = false;
std::string field_names_() const;
};

// Creates a new expression builder suitable for creating rules.
absl::StatusOr<std::unique_ptr<google::api::expr::runtime::CelExpressionBuilder>> NewRuleBuilder(
google::protobuf::Arena* arena);
Expand Down
4 changes: 2 additions & 2 deletions deps/shared_deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
},
"protovalidate": {
"meta": {
"version": "1.0.0-rc.1"
"version": "1.0.0-rc.2"
},
"source": {
"sha256": "f04ecb455e58172bcd7b011dac4ef35ffbe4f6acf0ec7021433bed4e2289de2b",
"sha256": "b379378e55d27111e5a3712c35aa126e58cbdd974ffcd3982c2d16f41bc01231",
"strip_prefix": "protovalidate-{version}",
"urls": [
"https://github.com/bufbuild/protovalidate/releases/download/v{version}/protovalidate-{version}.tar.gz"
Expand Down
Loading