Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference field in nested map in Firestore security rules

I have this data structure:

enter image description here

and need a rule that only allows updates when the city attribute is not changed:

match /requests/{requestId} {
  allow update: if (request.resource.data.diff(resource.data).unchangedKeys().
     hasAll(["customerlocation.city"]))

That doesn't work, because the diff result only shows the customerlocation field, but not the city attribute.

Is it even possible to achieve what I want? I know there are limitations wrt what you can do with nested objects in security rules, but I have might just missed something.

like image 426
Carsten Hagemann Avatar asked Oct 29 '25 13:10

Carsten Hagemann


1 Answers

I believe that the solution Aion proposed is the only one that will work if you don't want to change your data structure and have to do this inside of the Firestore rules.

I don't know what your overall data structure looks like, but you could move customerLocation out to its own collection. Then you would store a DocumentReference to customerLocation inside of the request.

You could also create a cloud function with document('requests/{requestId}').onUpdate() and check if the city changed. If it has, then change it back in there. This would require you to keep track of the fact that you changed back the property though. Otherwise, the function would keep triggering and flipping the values.

like image 112
nVitius Avatar answered Oct 31 '25 12:10

nVitius



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!