Olivia
Olivia

Reputation: 2161

firestore security rules - how to print out the rule values?

How do we check what the values are in firebase security rules? I have some rules that are failing but they shouldn't be so I need to check what is actually happening in there. Is there a console.log() or console.print() ability?

match /test/{testId} {
  //console.log(resource.data.relationships.keys());
  allow read: if resource.data.relationships.keys().hasAll([request.auth.uid]);
  allow write: if userIsAuthenticated();
}

Upvotes: 1

Views: 1373

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598563

If you're using the Firestore emulator, what you're looking for is the debug function, which:

  1. Logs the value passed in, and then
  2. Returns that value

Note:

debug function blocks are only executed by the Security Rules engine in the Firestore emulator, part of the Firebase Emulator Suite. The debug function has no effect in production.

You can use it anywhere in your rule value, so all of these are valid:

  • if debug(resource.data.relationships.keys().hasAll([request.auth.uid]))
  • if resource.data.relationships.keys().hasAll([debug(request.auth.uid)])

I'm curious if this is also valid:

  • if debug(resource.data.relationships.keys()).hasAll([request.auth.uid])

Let me know if you've given that a try.

Upvotes: 4

Related Questions