【问题标题】:Firebase validate with newDataFirebase 使用 newData 进行验证
【发布时间】:2015-11-19 03:23:12
【问题描述】:

我正在研究如何构建数据的出色示例,如 Kato 在这篇文章中所示: Firebase user account security with firebaseSimpleLogin

我没有任何运气让验证正常工作。

数据结构为:

accepted_invites
    game1
       desc  "fun game"

 invites
     game1
       uuidAAA    true
       uuidBBB    true

这是一个屏幕截图: Firebase data

如果我尝试编写以下内容

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

它将使用此规则在接受邀请中进行条目:

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

但不是

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

我尝试使用模拟器,但我得到了 类型错误:+ 只对数字和字符串起作用。

这是我拥有的完整规则:

{
  "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()"
      }
    }
  }
}
}

【问题讨论】:

    标签: firebase


    【解决方案1】:

    newData 关键字是指所有传入数据的特殊属性,但在 path 表达式中不允许使用它,因为它可能不是字符串。也就是说,它很可能是一个对象。

    如果您有兴趣在路径中使用该数据的某些部分,我建议您在更深的路径中包含另一个验证规则,例如:

    {
      "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,这行得通,现在规则对我来说更有意义了。
    猜你喜欢
    • 1970-01-01
    • 2020-10-09
    • 2021-05-26
    • 2017-09-12
    • 2021-07-20
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多