Search code examples
firebasegoogle-cloud-firestorefirebase-security

can you check the size of object keys in firestore similar to an array using .size()?


I am trying to created and object that will have a key and a related array to that key, the idea is I want to limit the amount of keys so it wont pass 10 and if it does the write will fail can I implement this using security rules ?

const obj = {
key1 : []
key2 : []
key3 : []
...rest
key10 : []
}

sure I can do client side block but want to make sure this object wont grow indefinity.


Solution

  • A Map also has size() method that returns number of keys in it. The following should work:

    match /images/{snippetId} {
      allow write: if request.resource.data.mapField.size() <= 10;
    }
    

    This will reject write operations if mapField has more than 10 keys.