Search code examples
google-cloud-firestorefirebase-security

Firestore security rules read array length


I created my Firestore rules

     match /users/{userId}/{document=**} {
   
      allow read, 
            update, 
            delete: if request.auth != null 
                    && request.auth.uid == userId;
      allow create: if request.auth != null 
                    && request.auth.uid == userId
                    && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role<6
                  //&& get(/databases/$(database)/documents/users/$(request.auth.uid)).data.roles_array.length <6
    }

and the line

&& get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role<6

works OK, but the line with array length

&& get(/databases/$(database)/documents/users/$(request.auth.uid)).data.roles_array.length <6

doesn't work and I have permission error

Below database screen

enter image description here


Solution

  • You need to use .size() to get length of array. Try:

    get(<DOC_PATH>).data.roles_array.size() < 6;