1

I'm working thru the excellent example on how to structure data as shown by Kato in this post: Firebase user account security with firebaseSimpleLogin

I'm not having any luck getting validate to work properly.

The data structure is :

accepted_invites
    game1
       desc  "fun game"

 invites
     game1
       uuidAAA    true
       uuidBBB    true

here's a screen shot: Firebase data

If I try and write the following

   ref.child("accepted_invites").child("game1").child("userTwo").child("uuidBBB").setValue(true);

it will make an entry in accepted_invites with this rule :

".validate": "root.child('invites/'+$game_id+'/uuidBBB').exists()"

but not

".validate": "root.child('invites/'+$game_id+'/'+newData.val()).exists()"

I tried using the simulator but I'm getting Type Error: + only operates on numbers and strings.

Here's the complete rules as I have them:

{
  "rules": {   
    ".write" : true,
    ".read" : true,
  "accepted_invites": {
    "$game_id": {
      "$user_id": {
        //This validate rule fails  
        //".validate": "root.child('invites/'+$game_id+'/'+newData.val()).exists()"
        //This one works
        ".validate": "root.child('invites/'+$game_id+'/uuidBBB').exists()"
      }
    }
  }
}
}
Community
  • 1
  • 1
VladimirSD
  • 401
  • 3
  • 11

1 Answers1

0

The newData keyword is a special attribute referring to all incoming data, but it is not permitted in path expressions because it may not be a string. i.e., it could very well be an object.

If you're interested in using some portion of that data within the path, I would recommend just including another validation rule at a deeper path, such as:

{
  "rules": {   
    ".write" : true,
    ".read" : true,
    "accepted_invites": {
      "$game_id": {
        "$accepting_user_id": {
          "$sending_user_id": {
            ".validate": "root.child('invites').child($game_id).child($sending_user_id).exists()"
          }
        }
      }
    }
  }
}
Rob DiMarco
  • 13,226
  • 1
  • 43
  • 55