Search code examples
firebasegoogle-cloud-firestorefirebase-security

What is the difference if you write the security rules in these 2 ways?


The first method shown below is from the documentation. The second method just takes the landmarks match statement out of the cities one. Is there a difference? I'm using rules version 2, but was looking for an answer for both rules versions.

service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      allow read, write: if <condition>;

        match /landmarks/{landmark} {
          allow read, write: if <condition>;
        }
    }
  }
}

vs

service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      allow read, write: if <condition>;
    }
    match /cities/{city}/landmarks/{landmark} {
      allow read, write: if <condition>;
    }
  }
}

Solution

  • As you've shared them, there is no functional difference between the two rule sets as far as I can tell at first glance.

    I typically prefer the first syntax, as it allows you to define functions in the city scope that are then available in nested rules too.