What errors can occur in the firebase realtime database security rule console when setting my rules and what are they?
What errors can occur in the firebase realtime database security rule console when setting my rules and what are they?
Error saving rules - Line n: syntax error
As the name itself says, syntax errors like, use |||
or &&&
instead of ||
or &&
, for example:
".write": "data.exists() &&& auth.uid == data.child('usuario').val() ||| !data.exists()
Error saving rules - Line n: Expected
This error is very simple, as the name itself says something is expected, a parentheses, braces, brackets or comma, for example:
".validate": "newData.hasChildren(['nome', 'idade')
Missing brackets after 'idade'
".validate": "newData.hasChildren(['nome', 'idade']
Missing parentheses after 'idade']
"rules":
".read": true
}
The keys are missing after "rules":
".read": true
".write": true
A comma is missing after the first true
to indicate the next chave: valor
Error saving rules - Line n: Unexpected end of expression
This error is caused by the quotation marks, 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 when using double quotation marks inside other double quotes, use this only at the beginning and end of the expressions, and within them use single quotation marks
You should also be careful, because you are not used to it, you may want to do something like
".read": "data.child("+$chave+").val() == true"
Same variables ( $
) should be enclosed in double quotation marks
Error saving rules - Line n: Rule expressions may not contain operator
This error is caused by changing &&
or ||
by &
or |
, I do not know of another situation that causes this type of error, for example:
".write": "data.exists() & auth.uid == data.child('usuario').val() | !data.exists()
Error saving rules - Line n: missing; before statement
This error is similar to the previous one, but in this case it occurs when an operator ( &&
or ||
) is not used, for example:
".write": "data.exists() auth.uid == data.child('usuario').val()"
Error saving rules - Line n: Rule expressions may not contain multiple expressions
Seeing the previous problem you may think is simple, just with ;
there, but not this causes another error. This means that the security rules do not support multiple expressions ( ;
is meant for this when used in the middle of expressions), for example:
".write": "data.exists(); auth.uid == data.child('usuario').val()"
The ;
can be used in the final of expressions, however I do not recommend it, besides being unnecessary, you may forget that you added it and, when adding a new condition, (half an hour looking for where the problem is), for example:
".write": "true;"
However, it causes an error when using double quotation marks, for example:
".write": true;
Error 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 can not assign values to something ( =
) in security rules, for example:
".read": "data.child('usuario').val() = auth.uid"
Error saving rules - Line n: Invalid increment / decrement operand
Just like you can not do value assignments to variables with the =
operator, you can not incrementally ( ++
) or decrement ( --
), 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 sum ( +
), subtraction ( -
), multiplication ( *
) or division ( /
) operators, for example:
".read": "data.child('quantidade').val() == newData.child('quantidade').val() + 1"
Error 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 errors happen when this does not happen. The difference between the two errors is that the second one specifies where the error is (right or 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 to something like != true
or === true