Dmytro Rostopira
Dmytro Rostopira

Reputation: 11175

Get current timestamp in firestore rules

I'm writing firestore rules function, which must return true, if certain value is between 0 and 1000 or bigger than current unix-timestamp. According to reference, I'm trying to do the following:

rules_version = '2';
service cloud.firestore {
  match /databases/{db}/documents {
    function check(i) {
      return i > 0 && (i < 1000 || i > (timestamp.epochMillis));
    }
// ...

However, rules editor gives me an error:

timestamp is a package and cannot be used as variable name.

Upvotes: 5

Views: 1204

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83153

I think that you should be able to use request.time as follows (untested):

return i > 0 && (i < 1000 || i > request.time.toMillis());

As explained here, with request.time you get a non-null rules.Timestamp:

When the request was received by the service.

For Firestore write operations that include server-side timestamps, this time will be equal to the server timestamp.

Upvotes: 5

Related Questions