Error saving rules in firebase console

Asked

Viewed 542 times

-1

What errors can occur in the firebase Realtime database security rules console when defining my rules and what they are?

1 answer

1


Error while saving rules - Line n: syntax error

As the name says, they are syntax errors like, use ||| or &&& instead of || or &&, for example:

".write": "data.exists() &&& auth.uid == data.child('usuario').val() ||| !data.exists()


Error while saving rules - Line n: Expected

This error is very simple, as the name itself says is expected something, a parentheses, keys, brackets or comma, for example:

".validate": "newData.hasChildren(['nome', 'idade')

One square bracket missing after 'idade'

".validate": "newData.hasChildren(['nome', 'idade']

One parentheses missing after 'idade']

"rules": 
    ".read": true
}

Missing to open keys after "rules":

".read": true
".write": true

Missing a comma after the first true to indicate the next chave: valor


Error while saving rules - Line n: Unexpected end of Expression

This error is caused by the quotation marks of the expressions, for example:

".read": "auth.uid == data.child('usuario').val()

Missing a double quotation mark at the end of the expression

".read": "auth.uid == data.child("usuario").val()"

The error is using double quotes inside other double quotes, use this only at the beginning and end of expressions, and inside them use single quotes

You should also be careful, because you’re not used to it, maybe you want to do something like

".read": "data.child("+$chave+").val() == true"

Same variables ($) should be inside double quotes


Error while saving rules - Line n: Rule Expressions may not contain Operator

This error is caused when switching && or || for & or |, I don’t know any other situation that causes this kind of mistake, for example:

".write": "data.exists() & auth.uid == data.child('usuario').val() | !data.exists()


Error while saving rules - Line n: Missing ; before statement

This error is similar to the previous one, but in this case it occurs when an operator is not used (&& or ||), for example:

".write": "data.exists() auth.uid == data.child('usuario').val()"


Error while saving rules - Line n: Rule Expressions may not contain Multiple Expressions

When seeing the previous problem you may think it is simple, just lap ; there, but this does not cause another error. This means that the security rules do not support multiple expressions (the ; serves this purpose when used in the middle of expressions), for example:

".write": "data.exists(); auth.uid == data.child('usuario').val()"

The ; can be used in final of expressions, but do not recommend, not only unnecessary, it is possible to forget that put and, adding a new condition, cause an error in your rergas (half an hour looking for where the problem is), for example:

".write": "true;"

But causes error when using without double quotes, for example:

".write": true;


Error while saving rules - Line n: Unexpected EOF

This error is caused by missing some key in the rules, for example:

"rules": {
    ".read": true


Error saving rules - Line n: Rule Expressions may not contain assignments

You cannot assign values to something (=) safety rules, for example:

".read": "data.child('usuario').val() = auth.uid"


Error saving rules - Line n: Invalid increment/decrement operand

Just as you cannot assign values to variables with the operator =, also cannot with increment (++) or decrease (--), for example:

".read": "data.child('quantidade').val() == newData.child('quantidade').val()++"

or

".read": "data.child('quantidade').val() == newData.child('quantidade').val()--"

But you can use the sum operators (+), subtraction (-), multiplication (*) or division (/), for example:

".read": "data.child('quantidade').val() == newData.child('quantidade').val() + 1"


Error while saving rules - Line n: Expression must evaluate to a Boolean

or

Error saving rules - Line n: left/right operand of &&/|| must be Boolean

The basis of firebase security rules is that they always return true or false, these mistakes happen when this does not happen. The difference between the two errors is that the second specifies where the error is (the right or the left of another comparison), for example:

".write": "data.exists() && data.child('usuario').val()"

Even though data.child('usuario').val() is a boolean value you should compare with something, like != true or === true

Browser other questions tagged

You are not signed in. Login or sign up in order to post.