Search code examples
firebasefirebase-authenticationfirebase-storagefirebase-security

How to iterate a list in Firebase Security Rules language?


I have the following condition written in Firebase Storage Rules:

allow read: if resource.metadata.company.includes(request.auth.uid);

This returns the following error:

Error running simulation – Error: simulator.rules line [9], column [22]. Function not found error: Name: [includes].

What I want to do is to iterate through a list and check if an object exists in that list; something that resembles includes(). How to do that in Firebase Security Rules Language?

NOTE: Already tried for loops and they don't work.


Solution

  • Found a solution for it by using string.matches('.*'+resource.metadata.company+'.*'). Why am I using matches? That is because my array is a JSON string. Hope this helps other people.

    Check documentation: https://firebase.google.com/docs/reference/rules/rules.String#matches