Skip to content
Open
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", repo_name = "com_github_bufbuild_protovalidate")
bazel_dep(name = "protovalidate", version = "1.1.1", repo_name = "com_github_bufbuild_protovalidate")
8 changes: 4 additions & 4 deletions MODULE.bazel.lock

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

1 change: 1 addition & 0 deletions buf/validate/internal/cel_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ MAP_RULES_TO_FIELD_NUMBER(RepeatedRules, FieldRules::kRepeatedFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(MapRules, FieldRules::kMapFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(AnyRules, FieldRules::kAnyFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(DurationRules, FieldRules::kDurationFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(FieldMaskRules, FieldRules::kFieldMaskFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(TimestampRules, FieldRules::kTimestampFieldNumber)

#undef MAP_RULES_TO_FIELD_NUMBER
Expand Down
8 changes: 8 additions & 0 deletions buf/validate/internal/cel_validation_rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ absl::Status CelValidationRules::Add(
return Add(builder, rule, std::move(rulePath), ruleField);
}

absl::Status CelValidationRules::Add(
google::api::expr::runtime::CelExpressionBuilder& builder,
std::string_view expression,
absl::optional<FieldPath> rulePath,
const google::protobuf::FieldDescriptor* ruleField) {
return Add(builder, expression, "", expression, std::move(rulePath), ruleField);
}

absl::Status CelValidationRules::ValidateCel(
RuleContext& ctx, google::api::expr::runtime::Activation& activation) const {
activation.InsertValue("rules", rules_);
Expand Down
5 changes: 5 additions & 0 deletions buf/validate/internal/cel_validation_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class CelValidationRules : public ValidationRules {
std::string_view expression,
absl::optional<FieldPath> rulePath,
const google::protobuf::FieldDescriptor* ruleField);
absl::Status Add(
google::api::expr::runtime::CelExpressionBuilder& builder,
std::string_view expression,
absl::optional<FieldPath> rulePath,
const google::protobuf::FieldDescriptor* ruleField);

// Validate all the cel rules given the activation that already has 'this' bound.
absl::Status ValidateCel(
Expand Down
28 changes: 28 additions & 0 deletions buf/validate/internal/field_rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@ absl::StatusOr<std::unique_ptr<FieldValidationRules>> NewFieldRules(
}
}
break;
case FieldRules::kFieldMask:
if (field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE ||
field->message_type()->full_name() !=
google::protobuf::FieldMask::descriptor()->full_name()) {
return absl::InvalidArgumentError("field_mask field validator on non-field_mask field");
} else {
auto result = std::make_unique<FieldValidationRules>(field, fieldLvl);
auto status = BuildCelRules(
messageFactory, allowUnknownFields, arena, builder, fieldLvl.field_mask(), *result);
if (!status.ok()) {
rules_or = status;
} else {
rules_or = std::move(result);
}
}
break;
case FieldRules::kTimestamp:
if (field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE ||
field->message_type()->full_name() !=
Expand Down Expand Up @@ -341,6 +357,18 @@ absl::StatusOr<std::unique_ptr<FieldValidationRules>> NewFieldRules(
absl::StrFormat("unknown field validator type %d", fieldLvl.type_case()));
}
if (rules_or.ok()) {
for (int i = 0; i < fieldLvl.cel_expression_size(); i++) {
const auto& expr = fieldLvl.cel_expression(i);
FieldPathElement celElement =
staticFieldPathElement<FieldRules, FieldRules::kCelExpressionFieldNumber>();
celElement.set_index(i);
FieldPath rulePath;
*rulePath.mutable_elements()->Add() = celElement;
auto status = rules_or.value()->Add(builder, expr, rulePath, nullptr);
if (!status.ok()) {
return status;
}
}
for (int i = 0; i < fieldLvl.cel_size(); i++) {
const auto& rule = fieldLvl.cel(i);
FieldPathElement celElement =
Expand Down
5 changes: 5 additions & 0 deletions buf/validate/internal/message_rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ namespace buf::validate::internal {
absl::StatusOr<std::unique_ptr<MessageValidationRules>> BuildMessageRules(
google::api::expr::runtime::CelExpressionBuilder& builder, const MessageRules& rules) {
auto result = std::make_unique<MessageValidationRules>();
for (const auto& expr: rules.cel_expression()) {
if (auto status = result->Add(builder, expr, absl::nullopt, nullptr); !status.ok()) {
return status;
}
}
for (const auto& rule : rules.cel()) {
if (auto status = result->Add(builder, rule, absl::nullopt, nullptr); !status.ok()) {
return status;
Expand Down
2 changes: 2 additions & 0 deletions buf/validate/internal/rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ absl::Status FieldValidationRules::Validate(
std::move(violation),
ProtoField{&message, field_},
ProtoField{&fieldRules_, requiredField});
return absl::OkStatus();
}
}
} else if (field_->is_repeated()) {
Expand All @@ -132,6 +133,7 @@ absl::Status FieldValidationRules::Validate(
std::move(violation),
ProtoField{&message, field_},
ProtoField{&fieldRules_, requiredField});
return absl::OkStatus();
}
}
} else {
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"
"version": "1.1.1"
},
"source": {
"sha256": "39a3f262614ffc23e89507f4dbdae535d8d73021713f580733b6a8dfdf06f734",
"sha256": "297dba5cdc25fa478c597e53af6c4ccf330c826e566384774b5176c13c8d3bd8",
"strip_prefix": "protovalidate-{version}",
"urls": [
"https://github.com/bufbuild/protovalidate/releases/download/v{version}/protovalidate-{version}.tar.gz"
Expand Down
Loading
Loading