@@ -28,7 +28,6 @@ namespace {
2828
2929absl::Status ProcessConstraint (
3030 ConstraintContext& ctx,
31- absl::string_view fieldPath,
3231 const google::api::expr::runtime::BaseActivation& activation,
3332 const CompiledConstraint& expr) {
3433 auto result_or = expr.expr ->Evaluate (activation, ctx.arena );
@@ -40,17 +39,21 @@ absl::Status ProcessConstraint(
4039 if (!result.BoolOrDie ()) {
4140 // Add violation with the constraint message.
4241 Violation& violation = *ctx.violations .add_violations ();
43- *violation.mutable_field_path () = fieldPath;
4442 violation.set_message (expr.constraint .message ());
4543 violation.set_constraint_id (expr.constraint .id ());
44+ if (expr.rulePath .has_value ()) {
45+ *violation.mutable_rule () = *expr.rulePath ;
46+ }
4647 }
4748 } else if (result.IsString ()) {
4849 if (!result.StringOrDie ().value ().empty ()) {
4950 // Add violation with custom message.
5051 Violation& violation = *ctx.violations .add_violations ();
51- *violation.mutable_field_path () = fieldPath;
5252 violation.set_message (std::string (result.StringOrDie ().value ()));
5353 violation.set_constraint_id (expr.constraint .id ());
54+ if (expr.rulePath .has_value ()) {
55+ *violation.mutable_rule () = *expr.rulePath ;
56+ }
5457 }
5558 } else if (result.IsError ()) {
5659 const cel::runtime::CelError& error = *result.ErrorOrDie ();
@@ -85,6 +88,7 @@ cel::runtime::CelValue ProtoFieldToCelValue(
8588absl::Status CelConstraintRules::Add (
8689 google::api::expr::runtime::CelExpressionBuilder& builder,
8790 Constraint constraint,
91+ absl::optional<FieldPath> rulePath,
8892 const google::protobuf::FieldDescriptor* rule) {
8993 auto pexpr_or = cel::parser::Parse (constraint.expression ());
9094 if (!pexpr_or.ok ()) {
@@ -96,7 +100,7 @@ absl::Status CelConstraintRules::Add(
96100 return expr_or.status ();
97101 }
98102 std::unique_ptr<cel::runtime::CelExpression> expr = std::move (expr_or).value ();
99- exprs_.emplace_back (CompiledConstraint{std::move (constraint), std::move (expr), rule});
103+ exprs_.emplace_back (CompiledConstraint{std::move (constraint), std::move (expr), std::move (rulePath), rule});
100104 return absl::OkStatus ();
101105}
102106
@@ -105,17 +109,17 @@ absl::Status CelConstraintRules::Add(
105109 std::string_view id,
106110 std::string_view message,
107111 std::string_view expression,
112+ absl::optional<FieldPath> rulePath,
108113 const google::protobuf::FieldDescriptor* rule) {
109114 Constraint constraint;
110115 *constraint.mutable_id () = id;
111116 *constraint.mutable_message () = message;
112117 *constraint.mutable_expression () = expression;
113- return Add (builder, constraint, rule);
118+ return Add (builder, constraint, std::move (rulePath), rule);
114119}
115120
116121absl::Status CelConstraintRules::ValidateCel (
117122 ConstraintContext& ctx,
118- std::string_view fieldName,
119123 google::api::expr::runtime::Activation& activation) const {
120124 activation.InsertValue (" rules" , rules_);
121125 activation.InsertValue (" now" , cel::runtime::CelValue::CreateTimestamp (absl::Now ()));
@@ -126,7 +130,7 @@ absl::Status CelConstraintRules::ValidateCel(
126130 activation.InsertValue (
127131 " rule" , ProtoFieldToCelValue (rules_.MessageOrDie (), expr.rule , ctx.arena ));
128132 }
129- status = ProcessConstraint (ctx, fieldName, activation, expr);
133+ status = ProcessConstraint (ctx, activation, expr);
130134 if (ctx.shouldReturn (status)) {
131135 break ;
132136 }
0 commit comments