Skip to content

Commit e569c58

Browse files
committed
Support cel_expression
Signed-off-by: Sri Krishna <skrishna@buf.build>
1 parent 9589499 commit e569c58

9 files changed

Lines changed: 60 additions & 68 deletions

File tree

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.0.0
1+
8.4.2

MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ bazel_dep(name = "abseil-cpp", version = "20240722.0", repo_name = "com_google_a
3434
bazel_dep(name = "cel-cpp", version = "0.11.0", repo_name = "com_google_cel_cpp")
3535

3636
bazel_dep(name = "protovalidate", version = "1.0.0", repo_name = "com_github_bufbuild_protovalidate")
37+
38+
git_override(module_name = "protovalidate", remote = "https://github.com/bufbuild/protovalidate", commit = "774f3764e09fcfc921b3ef5a42271754f0b7063a")

MODULE.bazel.lock

Lines changed: 21 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MODULE.bazel.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ bazel_dep(name = "abseil-cpp", version = "{{absl.meta.version}}", repo_name = "c
3434
bazel_dep(name = "cel-cpp", version = "{{cel_cpp.meta.version}}", repo_name = "com_google_cel_cpp")
3535

3636
bazel_dep(name = "protovalidate", version = "{{protovalidate.meta.version}}", repo_name = "com_github_bufbuild_protovalidate")
37+
38+
git_override(module_name = "protovalidate", remote = "https://github.com/bufbuild/protovalidate", commit = "774f3764e09fcfc921b3ef5a42271754f0b7063a")

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ BIN := .tmp/bin
1010
COPYRIGHT_YEARS := 2023-2025
1111
LICENSE_IGNORE := -e internal/testdata/
1212
LICENSE_HEADER_VERSION := 0294fdbe1ce8649ebaf5e87e8cdd588e33730bbb
13-
PROTOVALIDATE_VERSION ?= v$(shell <./deps/shared_deps.json jq -j .protovalidate.meta.version)
13+
# PROTOVALIDATE_VERSION ?= v$(shell <./deps/shared_deps.json jq -j .protovalidate.meta.version)
14+
PROTOVALIDATE_VERSION ?= 774f3764e09fcfc921b3ef5a42271754f0b7063a
1415

1516
# Set to use a different compiler. For example, `GO=go1.18rc1 make test`.
1617
GO ?= go

buf/validate/internal/cel_validation_rules.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ absl::Status CelValidationRules::Add(
125125
return Add(builder, rule, std::move(rulePath), ruleField);
126126
}
127127

128+
absl::Status CelValidationRules::Add(
129+
google::api::expr::runtime::CelExpressionBuilder& builder,
130+
std::string_view expression,
131+
absl::optional<FieldPath> rulePath,
132+
const google::protobuf::FieldDescriptor* ruleField) {
133+
Rule rule;
134+
*rule.mutable_expression() = expression;
135+
return Add(builder, rule, std::move(rulePath), ruleField);
136+
}
137+
128138
absl::Status CelValidationRules::ValidateCel(
129139
RuleContext& ctx, google::api::expr::runtime::Activation& activation) const {
130140
activation.InsertValue("rules", rules_);

buf/validate/internal/cel_validation_rules.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class CelValidationRules : public ValidationRules {
5151
std::string_view expression,
5252
absl::optional<FieldPath> rulePath,
5353
const google::protobuf::FieldDescriptor* ruleField);
54+
absl::Status Add(
55+
google::api::expr::runtime::CelExpressionBuilder& builder,
56+
std::string_view expression,
57+
absl::optional<FieldPath> rulePath,
58+
const google::protobuf::FieldDescriptor* ruleField);
5459

5560
// Validate all the cel rules given the activation that already has 'this' bound.
5661
absl::Status ValidateCel(

buf/validate/internal/field_rules.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ absl::StatusOr<std::unique_ptr<FieldValidationRules>> NewFieldRules(
341341
absl::StrFormat("unknown field validator type %d", fieldLvl.type_case()));
342342
}
343343
if (rules_or.ok()) {
344+
for (int i = 0; i < fieldLvl.cel_expression_size(); i++) {
345+
const auto& expr = fieldLvl.cel_expression(i);
346+
FieldPathElement celElement =
347+
staticFieldPathElement<FieldRules, FieldRules::kCelExpressionFieldNumber>();
348+
celElement.set_index(i);
349+
FieldPath rulePath;
350+
*rulePath.mutable_elements()->Add() = celElement;
351+
auto status = rules_or.value()->Add(builder, expr, rulePath, nullptr);
352+
if (!status.ok()) {
353+
return status;
354+
}
355+
}
344356
for (int i = 0; i < fieldLvl.cel_size(); i++) {
345357
const auto& rule = fieldLvl.cel(i);
346358
FieldPathElement celElement =

buf/validate/internal/message_rules.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ namespace buf::validate::internal {
2424
absl::StatusOr<std::unique_ptr<MessageValidationRules>> BuildMessageRules(
2525
google::api::expr::runtime::CelExpressionBuilder& builder, const MessageRules& rules) {
2626
auto result = std::make_unique<MessageValidationRules>();
27+
for (const auto& expr: rules.cel_expression()) {
28+
if (auto status = result->Add(builder, expr, absl::nullopt, nullptr); !status.ok()) {
29+
return status;
30+
}
31+
}
2732
for (const auto& rule : rules.cel()) {
2833
if (auto status = result->Add(builder, rule, absl::nullopt, nullptr); !status.ok()) {
2934
return status;

0 commit comments

Comments
 (0)