syntax = "proto2";
// apparently we need to use either proto2 or Protobuf 2023 Edition for "predefined rules"
// See https://protovalidate.com/schemas/predefined-rules/#create-a-rule-file

package nominal.procedures.v1;

import "buf/validate/validate.proto";

option java_multiple_files = true;
option java_package = "io.nominal.procedures.v1";

extend buf.validate.StringRules {
  optional bool valid_step_identifier = 1000 [(buf.validate.predefined).cel = {
    id: "string.valid_step_identifier"
    message: "must be a valid step identifier"
    expression:
      "this.matches('^[A-Za-z_][A-Za-z0-9_]*$')"
      // "this" is a reserved step name
      "&& !['this'].exists(r, r == this)"
  }];

  optional bool valid_field_identifier = 1001 [(buf.validate.predefined).cel = {
    id: "string.valid_field_identifier"
    message: "must be a valid field identifier"
    expression:
      "this.matches('^[A-Za-z_][A-Za-z0-9_]*$')"
      // "started_at" and "completed_at" are reserved field names
      "&& !['started_at', 'completed_at'].exists(r, r == this)"
  }];

  optional bool valid_field_reference = 1002 [(buf.validate.predefined).cel = {
    id: "string.valid_field_reference"
    message: "must be either a valid field identifier or {identifier}.{identifier}"
    expression: /* field identifier -- identical to `valid_field_identifier` above */
      "("
      "this.matches('^[A-Za-z_][A-Za-z0-9_]*$')"
      "&& !['started_at', 'completed_at'].exists(r, r == this)"
      ")"
      "||"
      // two identifiers separated by '.' (escaping was not working, so wrapped the dot in a character class)
      "this.matches('^[A-Za-z_][A-Za-z0-9_]*[.][A-Za-z_][A-Za-z0-9_]*$')"
  }];
}
