Casting String to Int in Cloud Firestore Security

2019-07-12 17:20发布

问题:

I am coming from this question. I thought about starting a bounty on it because it has not received any attention in months and over the time I did not see any answers, but I think that my desire is a bit different.

If there is a number as a field of a document, but specified as a string:

A problem with Firestore Rules arises.

Checks like this become impossible:

resource.data.number > 11

It is not possible to compare strings as if they were ints.

The simulator exception is as follows:

Unsupported operation error. Received: string > int. Expected: constraint > constraint, int > int, float > float, duration > duration, timestamp > timestamp, constraint > any, any > constraint. 

Is there a way to convert my operation to int > int?

回答1:

Check out this link that shows you how to convert strings to integers in the Firestore rules docs.

int("2") == 2
int(2.0) == 2

So your check could be:

int(resource.data.number) > 11