Will
Will

Reputation: 2673

Function not found when converting string to boolean in Firestore security rules

I'm writing a security rule on Firestore that converts a string into a boolean. According to the documentation, we should be able to do it by using the bool function.

I've created the following rule:

match /posts/{postID} {
  allow get: if bool("true") == true;
}

That seems to be the same example we have in the docs. However, I'm getting a Function not found error: Name: [bool]. for 'get' @ L6 error when I run this rule using the emulator.

At first, I thought it could be a bug in the emulator. So, I tried the same rule on the Firebase Console too and it doesn't work either:

error on the firebase console

Is this a bug on Firebase or am I doing something wrong here?

Upvotes: 0

Views: 107

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

firebaser here

I checked with our engineering team who looked at the code for this part of the rules engine, and this may have been some wishful thinking on the part of the writer of that documentation: no built-in bool function exists.

The good news is that you can define a function like that yourself with:

function bool(str) {
  return str == "true" ? true : (str == "false" ? false : null);
}

We'll get the docs updated to either include this definition or to remove the mention of the bool function altogether.

Upvotes: 2

Related Questions