Skip to content

How to write a CEL rule to compare two DateTime fields? #463

@julioz

Description

@julioz

I've got the following protobuf file with protovalidate rules:

syntax = "proto3";

package playground;
	
import "buf/validate/validate.proto";
import "google/protobuf/timestamp.proto";
import "google/type/datetime.proto";

message BookingTimes {
  // checkin_time is the time of entry in the hotel.
  google.type.DateTime checkin_time = 1 [(buf.validate.field).required = true];
  // checkout_time is the time of leave of the hotel.
  // must be after checkin_time.
  google.type.DateTime checkout_time = 2;
}

How can I write a CEL rule with protovalidate to enforce that checkout_time is after checkin_time?

I understand google.type.DateTime is not a well-known type, so protovalidate doesn't reference those as it does for google.protobuf.Timestamp etc. Still, I couldn't find a well-documented path if I were to write my own set of library rules to distribute internally at my org. I've checked the source for https://github.com/bufbuild/cel-es for how the google.protobuf.Timestamp rules are defined, but also there it is not obvious how one would write custom rules.

I imagine landing on something like:

message BookingTimes {
  // checkin_time is the time of entry in the hotel.
  google.type.DateTime checkin_time = 1 [(buf.validate.field).required = true];
  // checkout_time is the time of leave of the hotel.
  // must be after checkin_time.
  google.type.DateTime checkout_time = 2  [(buf.validate.field).cel = {
    id: "checkout_after_checking"
    message: "the checkout must have happened after the check-in"
    expression:
      "this.checkin_time < this.checkout_time"
  }];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions