Search code examples
google-cloud-firestorefirebase-security

Firestore security rules: check a condition for every value in an array


In Firestore security rules, is there any way to check a condition for every value in an array?

I have a document that has a subcollection. The document has an order field which is an array of IDs of documents in the subcollection; this array defines a custom user-defined order for those documents.

I want a security rule that checks that any values added to the order array correspond to a document in the subcollection (i.e. that the document exists). That is, it needs to check this condition for every value in the array.


Solution

  • What you call an array is known as a List in Firestore security rules, and there are no list comprehension style operations beyond the has* checks for literal values.

    The problem is that such a looped check would quickly become a performance (and cost) bottle neck.