Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting String to Int in Cloud Firestore Security Rules

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:

screenshot

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?

like image 433
creativecreatorormaybenot Avatar asked Mar 05 '26 20:03

creativecreatorormaybenot


1 Answers

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
like image 89
JeremyW Avatar answered Mar 08 '26 15:03

JeremyW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!