0
"RegisterRequests": {
  "$uid": {
    ".write": "$uid === auth.uid && !root.child('Users').child(auth.uid).exists()",
    ".validate": "newData.hasChildren(['Nickname']) && newData.hasChildren(['Whatever'])",
    "Nickname": {
      ".validate": "newData.val().length > 1 && newData.val().length < 17 && !root.child('Users').child('Nickname').child(newData.val()).exists() && !root.child('RegisterRequests').child('Nickname').child(newData.val()).exists()"
    },
    "Whatever": {
    ".validate": "newData.val() > 0 && newData.val() < 6"
    },
    "$other": { ".validate": false }
  }
}

"Users": {
  "$uid": {
    ".read": "$uid === auth.uid",
    "Nickname": {

    }
  }
}

These are my rules for my database. I am trying to prevent adding data that contains a nickname already existing in Users or RegisterRequests. I think I fail here: !root.child('Users').child('Nickname').child(newData.val()).exists() && !root.child('RegisterRequests').child('Nickname').child(newData.val()).exists() because Nickname has value not children. But I don't know how to make this check. Also I don't know if this is a correct way to check for children anyway, because I am missing the Identifier child in this hierarchy. Can anyone help me?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
A. Newbie
  • 187
  • 1
  • 3
  • 17
  • To enforce unique nicknames, you should create a top-level collection `/nicknames` where the keys are the nicknames, and the value of each key is the UID of the "owner" of that nickname. Since keys are guaranteed to be unique, storing the nicknames as keys ensures that the nicknames are also unique. – Frank van Puffelen Aug 20 '18 at 23:22
  • See https://stackoverflow.com/questions/35243492/firebase-android-make-username-unique, the other answers linked from there, and many others. – Frank van Puffelen Aug 20 '18 at 23:23

0 Answers0