Long-story short, I would like unique roomIds and roomNames. Therefore, I would like to add a firebase validation rule to disallow any room object writes to the DB if the roomName exists.
This is the firebaseDb JSON:
{
"rooms" : {
"-M8z0o2NkOuAYoqa2Yhn" : {
"roomId" : "-M8z0o2NkOuAYoqa2Yhn", // push().getKey();
"roomName" : "blueBUCKET",
},
"-M8zBrzRYfGE-iZaIoAV" : {
"roomId" : "-M8zBrzRYfGE-iZaIoAV", // push().getKey()
"roomName" : "redBUCKET",
}
}
},
}
This is my intended validation:
"rooms" : {
"$roomId" : {
".read" : "auth !== null",
".write" : "!data.exists() ||
data.child('userList'+'/'+auth.uid).exists()",
".validate" : "root.child('rooms/'+$roomId+'/roomName').val() !== newData.child('roomName').val()"
}
}
Unfortunately, it does not exhibit the correct behavior when I used the rules playground. Given the below, the simulated set should be disallowed given a room 'blueBUCKET' already exists.
Location: /rooms/-M8zBrzRYfGE-iZaIoAV
Data: { "roomName": "blueBUCKET" }
Authenticated: Yes w. UserId: QwKjHPsLdrcOrkiOLRs6pYZTOnE2
To be thorough, I played around with the rules playground further with the above parameters and this was the behavior. Clearly my rule is unable to reconcile the $roomId wildcard... Did I misplace my 'validate' assertion? or is this a matter of re-writin the rule?
// PASSED AS EXPECTED
".validate" : "root.child('rooms/-M8zBrzRYfGE-iZaIoAV/roomName').val() === 'redBUCKET'"
".validate" : "root.child('rooms/'+$roomId+'/roomName').val() === 'redBUCKET'"
".validate" : "root.child('rooms/-M8z0o2NkOuAYoqa2Yhn/roomName').val() === 'blueBUCKET'"
// FAILED - UNKNOWN WHY?
".validate" : "root.child('rooms/'+$roomId+'/roomName').val() === 'blueBUCKET'"