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
6 changes: 2 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ archive_override(
module_name = "cel-cpp",
strip_prefix = "cel-cpp-0.11.0",
patches=[
"@protovalidate-cc//deps:patches/cel_cpp/0001-Allow-message-field-access-using-index-operator.patch",
"@protovalidate-cc//deps:patches/cel_cpp/0002-Add-missing-include-for-absl-StrCat.patch",
"@protovalidate-cc//deps:patches/cel_cpp/0003-Fix-build-on-Windows-MSVC.patch",
"@protovalidate-cc//deps:patches/cel_cpp/0001-Fix-build-on-Windows-MSVC.patch",
],
patch_args=["-p1"],
urls = [
"https://github.com/google/cel-cpp/archive/v0.11.0.tar.gz"
]
)

bazel_dep(name = "protovalidate", version = "0.10.6", repo_name = "com_github_bufbuild_protovalidate")
bazel_dep(name = "protovalidate", version = "0.11.0", 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.

4 changes: 1 addition & 3 deletions MODULE.bazel.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ archive_override(
module_name = "cel-cpp",
strip_prefix = "cel-cpp-{{cel_cpp.meta.version}}",
patches=[
"@protovalidate-cc//deps:patches/cel_cpp/0001-Allow-message-field-access-using-index-operator.patch",
"@protovalidate-cc//deps:patches/cel_cpp/0002-Add-missing-include-for-absl-StrCat.patch",
"@protovalidate-cc//deps:patches/cel_cpp/0003-Fix-build-on-Windows-MSVC.patch",
"@protovalidate-cc//deps:patches/cel_cpp/0001-Fix-build-on-Windows-MSVC.patch",
],
patch_args=["-p1"],
urls = [
Expand Down
4 changes: 1 addition & 3 deletions bazel/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ cc_library(
"com_google_cel_cpp": shared_dep(
name="cel_cpp",
patches=[
"@com_github_bufbuild_protovalidate_cc//deps:patches/cel_cpp/0001-Allow-message-field-access-using-index-operator.patch",
"@com_github_bufbuild_protovalidate_cc//deps:patches/cel_cpp/0002-Add-missing-include-for-absl-StrCat.patch",
"@com_github_bufbuild_protovalidate_cc//deps:patches/cel_cpp/0003-Fix-build-on-Windows-MSVC.patch",
"@com_github_bufbuild_protovalidate_cc//deps:patches/cel_cpp/0001-Fix-build-on-Windows-MSVC.patch",
],
patch_args=["-p1"],
),
Expand Down
2 changes: 1 addition & 1 deletion buf/validate/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cc_test(
deps = [
":validator",
"@com_github_bufbuild_protovalidate//proto/protovalidate-testing/buf/validate/conformance/cases:buf_validate_conformance_cases_proto_cc",
"@com_github_bufbuild_protovalidate//proto/protovalidate-testing/buf/validate/conformance/cases/custom_constraints:buf_validate_conformance_cases_custom_constraints_cc_proto",
"@com_github_bufbuild_protovalidate//proto/protovalidate-testing/buf/validate/conformance/cases/custom_rules:buf_validate_conformance_cases_custom_rules_cc_proto",
"@com_google_cel_cpp//eval/public:activation",
"@com_google_cel_cpp//eval/public:builtin_func_registrar",
"@com_google_cel_cpp//eval/public:cel_expr_builder_factory",
Expand Down
2 changes: 2 additions & 0 deletions buf/validate/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_cel_cpp//eval/public:cel_function_adapter",
"@com_google_cel_cpp//eval/public:cel_function_registry",
"@com_google_cel_cpp//eval/public/containers:field_access",
"@com_google_cel_cpp//eval/public/containers:field_backed_list_impl",
"@com_google_cel_cpp//eval/public/containers:container_backed_map_impl",
"@com_googlesource_code_re2//:re2",
],
Expand Down
8 changes: 4 additions & 4 deletions buf/validate/internal/cel_constraint_rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ absl::Status ProcessConstraint(
// Add violation with the constraint message.
Violation violation;
violation.set_message(expr.constraint.message());
violation.set_constraint_id(expr.constraint.id());
violation.set_rule_id(expr.constraint.id());
if (expr.rulePath.has_value()) {
*violation.mutable_rule() = *expr.rulePath;
}
Expand All @@ -51,7 +51,7 @@ absl::Status ProcessConstraint(
// Add violation with custom message.
Violation violation;
violation.set_message(std::string(result.StringOrDie().value()));
violation.set_constraint_id(expr.constraint.id());
violation.set_rule_id(expr.constraint.id());
if (expr.rulePath.has_value()) {
*violation.mutable_rule() = *expr.rulePath;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ cel::runtime::CelValue ProtoFieldToCelValue(

absl::Status CelConstraintRules::Add(
google::api::expr::runtime::CelExpressionBuilder& builder,
Constraint constraint,
Rule constraint,
absl::optional<FieldPath> rulePath,
const google::protobuf::FieldDescriptor* rule) {
auto pexpr_or = cel::parser::Parse(constraint.expression());
Expand All @@ -113,7 +113,7 @@ absl::Status CelConstraintRules::Add(
std::string_view expression,
absl::optional<FieldPath> rulePath,
const google::protobuf::FieldDescriptor* rule) {
Constraint constraint;
Rule constraint;
*constraint.mutable_id() = id;
*constraint.mutable_message() = message;
*constraint.mutable_expression() = expression;
Expand Down
4 changes: 2 additions & 2 deletions buf/validate/internal/cel_constraint_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace buf::validate::internal {

// A compiled constraint expression.
struct CompiledConstraint {
buf::validate::Constraint constraint;
buf::validate::Rule constraint;
std::unique_ptr<google::api::expr::runtime::CelExpression> expr;
const absl::optional<FieldPath> rulePath;
const google::protobuf::FieldDescriptor* rule;
Expand All @@ -41,7 +41,7 @@ class CelConstraintRules : public ConstraintRules {

absl::Status Add(
google::api::expr::runtime::CelExpressionBuilder& builder,
Constraint constraint,
Rule constraint,
absl::optional<FieldPath> rulePath,
const google::protobuf::FieldDescriptor* rule);
absl::Status Add(
Expand Down
44 changes: 22 additions & 22 deletions buf/validate/internal/cel_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ constexpr int ruleFieldNumber() = delete;
return fieldNumber; \
}

MAP_RULES_TO_FIELD_NUMBER(FloatRules, FieldConstraints::kFloatFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(DoubleRules, FieldConstraints::kDoubleFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(Int32Rules, FieldConstraints::kInt32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(Int64Rules, FieldConstraints::kInt64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(UInt32Rules, FieldConstraints::kUint32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(UInt64Rules, FieldConstraints::kUint64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(SInt32Rules, FieldConstraints::kSint32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(SInt64Rules, FieldConstraints::kSint64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(Fixed32Rules, FieldConstraints::kFixed32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(Fixed64Rules, FieldConstraints::kFixed64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(SFixed32Rules, FieldConstraints::kSfixed32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(SFixed64Rules, FieldConstraints::kSfixed64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(BoolRules, FieldConstraints::kBoolFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(StringRules, FieldConstraints::kStringFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(BytesRules, FieldConstraints::kBytesFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(EnumRules, FieldConstraints::kEnumFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(RepeatedRules, FieldConstraints::kRepeatedFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(MapRules, FieldConstraints::kMapFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(AnyRules, FieldConstraints::kAnyFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(DurationRules, FieldConstraints::kDurationFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(TimestampRules, FieldConstraints::kTimestampFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(FloatRules, FieldRules::kFloatFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(DoubleRules, FieldRules::kDoubleFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(Int32Rules, FieldRules::kInt32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(Int64Rules, FieldRules::kInt64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(UInt32Rules, FieldRules::kUint32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(UInt64Rules, FieldRules::kUint64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(SInt32Rules, FieldRules::kSint32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(SInt64Rules, FieldRules::kSint64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(Fixed32Rules, FieldRules::kFixed32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(Fixed64Rules, FieldRules::kFixed64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(SFixed32Rules, FieldRules::kSfixed32FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(SFixed64Rules, FieldRules::kSfixed64FieldNumber)
MAP_RULES_TO_FIELD_NUMBER(BoolRules, FieldRules::kBoolFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(StringRules, FieldRules::kStringFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(BytesRules, FieldRules::kBytesFieldNumber)
MAP_RULES_TO_FIELD_NUMBER(EnumRules, FieldRules::kEnumFieldNumber)
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(TimestampRules, FieldRules::kTimestampFieldNumber)

#undef MAP_RULES_TO_FIELD_NUMBER

Expand Down Expand Up @@ -97,7 +97,7 @@ absl::Status BuildCelRules(
FieldPath rulePath;
*rulePath.mutable_elements()->Add() = fieldPathElement(field);
*rulePath.mutable_elements()->Add() =
staticFieldPathElement<FieldConstraints, ruleFieldNumber<R>()>();
staticFieldPathElement<FieldRules, ruleFieldNumber<R>()>();
if (!field->options().HasExtension(buf::validate::predefined)) {
continue;
}
Expand Down
36 changes: 18 additions & 18 deletions buf/validate/internal/constraints.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ absl::Status MessageConstraintRules::Validate(
absl::Status FieldConstraintRules::Validate(
ConstraintContext& ctx, const google::protobuf::Message& message) const {
static const google::protobuf::FieldDescriptor* requiredField =
FieldConstraints::descriptor()->FindFieldByNumber(
FieldConstraints::kRequiredFieldNumber);
FieldRules::descriptor()->FindFieldByNumber(
FieldRules::kRequiredFieldNumber);
google::api::expr::runtime::Activation activation;
cel::runtime::CelValue result;
std::string subPath;
Expand All @@ -141,11 +141,11 @@ absl::Status FieldConstraintRules::Validate(
return absl::OkStatus();
} else if (required_) {
Violation violation;
*violation.mutable_constraint_id() = "required";
*violation.mutable_rule_id() = "required";
*violation.mutable_message() = "value is required";
*violation.mutable_field()->mutable_elements()->Add() = fieldPathElement(field_);
*violation.mutable_rule()->mutable_elements()->Add() =
staticFieldPathElement<FieldConstraints, FieldConstraints::kRequiredFieldNumber>();
staticFieldPathElement<FieldRules, FieldRules::kRequiredFieldNumber>();
ctx.violations.emplace_back(
std::move(violation),
ProtoField{&message, field_},
Expand All @@ -161,11 +161,11 @@ absl::Status FieldConstraintRules::Validate(
return absl::OkStatus();
} else if (required_) {
Violation violation;
*violation.mutable_constraint_id() = "required";
*violation.mutable_rule_id() = "required";
*violation.mutable_message() = "value is required";
*violation.mutable_field()->mutable_elements()->Add() = fieldPathElement(field_);
*violation.mutable_rule()->mutable_elements()->Add() =
staticFieldPathElement<FieldConstraints, FieldConstraints::kRequiredFieldNumber>();
staticFieldPathElement<FieldRules, FieldRules::kRequiredFieldNumber>();
ctx.violations.emplace_back(
std::move(violation),
ProtoField{&message, field_},
Expand All @@ -176,11 +176,11 @@ absl::Status FieldConstraintRules::Validate(
if (!message.GetReflection()->HasField(message, field_)) {
if (required_) {
Violation violation;
*violation.mutable_constraint_id() = "required";
*violation.mutable_rule_id() = "required";
*violation.mutable_message() = "value is required";
*violation.mutable_field()->mutable_elements()->Add() = fieldPathElement(field_);
*violation.mutable_rule()->mutable_elements()->Add() =
staticFieldPathElement<FieldConstraints, FieldConstraints::kRequiredFieldNumber>();
staticFieldPathElement<FieldRules, FieldRules::kRequiredFieldNumber>();
ctx.violations.emplace_back(
std::move(violation),
ProtoField{&message, field_},
Expand Down Expand Up @@ -235,11 +235,11 @@ absl::Status EnumConstraintRules::Validate(
auto value = message.GetReflection()->GetEnumValue(message, field_);
if (field_->enum_type()->FindValueByNumber(value) == nullptr) {
Violation violation;
*violation.mutable_constraint_id() = "enum.defined_only";
*violation.mutable_rule_id() = "enum.defined_only";
*violation.mutable_message() = "value must be one of the defined enum values";
*violation.mutable_field()->mutable_elements()->Add() = fieldPathElement(field_);
*violation.mutable_rule()->mutable_elements()->Add() = fieldPathElement(EnumRules::descriptor()->FindFieldByNumber(EnumRules::kDefinedOnlyFieldNumber));
*violation.mutable_rule()->mutable_elements()->Add() = fieldPathElement(FieldConstraints::descriptor()->FindFieldByNumber(FieldConstraints::kEnumFieldNumber));
*violation.mutable_rule()->mutable_elements()->Add() = fieldPathElement(FieldRules::descriptor()->FindFieldByNumber(FieldRules::kEnumFieldNumber));
ctx.violations.emplace_back(
std::move(violation),
ProtoField{&message, field_},
Expand Down Expand Up @@ -280,7 +280,7 @@ absl::Status RepeatedConstraintRules::Validate(
ctx.appendFieldPathElement(element, pos);
ctx.appendRulePathElement({
fieldPathElement(RepeatedRules::descriptor()->FindFieldByNumber(RepeatedRules::kItemsFieldNumber)),
fieldPathElement(FieldConstraints::descriptor()->FindFieldByNumber(FieldConstraints::kRepeatedFieldNumber)),
fieldPathElement(FieldRules::descriptor()->FindFieldByNumber(FieldRules::kRepeatedFieldNumber)),
}, pos);
ctx.setFieldValue(ProtoField{&message, field_, i}, pos);
}
Expand Down Expand Up @@ -320,7 +320,7 @@ absl::Status MapConstraintRules::Validate(
if (ctx.violations.size() > pos) {
ctx.appendRulePathElement({
fieldPathElement(MapRules::descriptor()->FindFieldByNumber(MapRules::kKeysFieldNumber)),
fieldPathElement(FieldConstraints::descriptor()->FindFieldByNumber(FieldConstraints::kMapFieldNumber)),
fieldPathElement(FieldRules::descriptor()->FindFieldByNumber(FieldRules::kMapFieldNumber)),
}, pos);
ctx.setFieldValue(ProtoField{&elemMsg, keyField}, pos);
ctx.setForKey(pos);
Expand All @@ -340,7 +340,7 @@ absl::Status MapConstraintRules::Validate(
if (ctx.violations.size() > valuePos) {
ctx.appendRulePathElement({
fieldPathElement(MapRules::descriptor()->FindFieldByNumber(MapRules::kValuesFieldNumber)),
fieldPathElement(FieldConstraints::descriptor()->FindFieldByNumber(FieldConstraints::kMapFieldNumber)),
fieldPathElement(FieldRules::descriptor()->FindFieldByNumber(FieldRules::kMapFieldNumber)),
}, valuePos);
ctx.setFieldValue(ProtoField{&elemMsg, valueField}, pos);
}
Expand Down Expand Up @@ -389,15 +389,15 @@ absl::Status FieldConstraintRules::ValidateAny(
}
if (!found) {
Violation violation;
*violation.mutable_constraint_id() = "any.in";
*violation.mutable_rule_id() = "any.in";
*violation.mutable_message() = "type URL must be in the allow list";
if (field.index() == -1) {
*violation.mutable_field()->mutable_elements()->Add() = fieldPathElement(field.descriptor());
}
*violation.mutable_rule()->mutable_elements()->Add() =
staticFieldPathElement<AnyRules, AnyRules::kInFieldNumber>();
*violation.mutable_rule()->mutable_elements()->Add() =
staticFieldPathElement<FieldConstraints, FieldConstraints::kAnyFieldNumber>();
staticFieldPathElement<FieldRules, FieldRules::kAnyFieldNumber>();
ctx.violations.emplace_back(
std::move(violation),
field,
Expand All @@ -407,15 +407,15 @@ absl::Status FieldConstraintRules::ValidateAny(
for (const auto& block : anyRules_->not_in()) {
if (block == typeUri) {
Violation violation;
*violation.mutable_constraint_id() = "any.not_in";
*violation.mutable_rule_id() = "any.not_in";
*violation.mutable_message() = "type URL must not be in the block list";
if (field.index() == -1) {
*violation.mutable_field()->mutable_elements()->Add() = fieldPathElement(field.descriptor());
}
*violation.mutable_rule()->mutable_elements()->Add() =
staticFieldPathElement<AnyRules, AnyRules::kNotInFieldNumber>();
*violation.mutable_rule()->mutable_elements()->Add() =
staticFieldPathElement<FieldConstraints, FieldConstraints::kAnyFieldNumber>();
staticFieldPathElement<FieldRules, FieldRules::kAnyFieldNumber>();
ctx.violations.emplace_back(
std::move(violation),
field,
Expand All @@ -432,7 +432,7 @@ absl::Status OneofConstraintRules::Validate(
if (required_) {
if (!message.GetReflection()->HasOneof(message, oneof_)) {
Violation violation;
*violation.mutable_constraint_id() = "required";
*violation.mutable_rule_id() = "required";
*violation.mutable_message() = "exactly one field is required in oneof";
*violation.mutable_field()->mutable_elements()->Add() = oneofPathElement(*oneof_);
ctx.violations.emplace_back(std::move(violation), absl::nullopt, absl::nullopt);
Expand Down
Loading
Loading