I have a security rule for adding new data
"CATEGORIES": {
"$CATEGORIES": {
".write": "root.child('USERS/' + auth.uid + '/type').val() == 'admin' && !data.exists() || root.child('USERS/' + auth.uid + '/type').val() == 'admin' && root.child('MODES/delete').val() == 'yes'",
"$DATA": {
".write": "root.child('USERS/' + auth.uid + '/type').val() == 'admin'",
}
}
}
It's used to prevent duplicates or overwriting of the child node with the !data.exists()
rule.
Problems is, it wont let me delete the child with this rule because data exists. To get around this I added the root.child('MODES/delete').val()
data to allow me to delete it if I set it to 'yes'.
How can I have it so I don't have to do this work around? So I can have no duplicates but also be able to delete the child if I want to?
To allow creates but no overwriting, !data.exists()
works fine. To allow deletes, you can use !newData.exists()
.
So all together:
// create or delete, but no update/overwrite
".write": "!data.exists() || !newData.exists()"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With