Skip to content

[BUG] Validation of timestamp fields fails on repeated calls to validate(). #185

Description

@PavloAvdyeyev-Ki

Description

Validator behaves like it caches the current time. When we do the validation twice, the second validation fails.

Steps to Reproduce

val validator = new Validator()
val objToValidateWithTimestamp1 = new MyObj(currentTime)
validator.validate(objToValidateWithTimestamp) // object is valid
val objToValidateWithTimestamp2 = new MyObj(currentTime)
validator.validate(objToValidateWithTimestamp) // object is not valid

The validation rules are:

google.protobuf.Timestamp my_field = 1 [(buf.validate.field).cel = {
    id: "my_field_validation"
    message: "Timestamp must be in the past."
    expression: "timestamp(this) < now"
  }]

or

google.protobuf.Timestamp my_field = 1 [(buf.validate.field).timestamp.lt_now = true];

if we recreate the object each validation it works fine:

val objToValidateWithTimestamp1 = new MyObj(currentTime)
new Validator().validate(objToValidateWithTimestamp) // object is valid
val objToValidateWithTimestamp2 = new MyObj(currentTime)
new Validator().validate(objToValidateWithTimestamp) // object is valid

Expected Behavior

val validator = new Validator()
val objToValidateWithTimestamp1 = new MyObj(currentTime)
validator.validate(objToValidateWithTimestamp) // object is valid
val objToValidateWithTimestamp2 = new MyObj(currentTime)
validator.validate(objToValidateWithTimestamp) // object is valid

Actual Behavior

val validator = new Validator()
val objToValidateWithTimestamp1 = new MyObj(currentTime)
validator.validate(objToValidateWithTimestamp) // object is valid
val objToValidateWithTimestamp2 = new MyObj(currentTime)
validator.validate(objToValidateWithTimestamp) // object is not valid

Screenshots/Logs

Environment

  • Operating System: macOS
  • Version: macOS 14.6.1
  • Compiler/Toolchain: JDK 17
  • Protovalidate Version: build.buf:protovalidate:0.3.2

Additional Context

for the successful validation I have to make deduction from the current timestamp of the validated field with increasing delta:

//first validation:
validator.validate({ts: currentTimestamp})
//10sec later:
validator.validate({ts: currentTimestamp - 10sec})
//1min later:
validator.validate({ts: currentTimestamp - 1min})

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions