【问题标题】:how to create required fields validation in firebase如何在firebase中创建必填字段验证
【发布时间】:2015-02-14 20:27:04
【问题描述】:

我的必填字段验证规则似乎在仪表板规则模拟器中工作,但当我真正尝试时,我的权限被拒绝。我设置正确了吗? (.msg 是消息文本)

{
    "rules": {
      "messages":{
        ".read": "auth.uid !== null",
        ".write": "auth.uid !== null",
        ".validate": "newData.child('msg').exists()",
        "uid": {
          ".validate": "auth.uid === newData.val()"
        }
      }
    }
}

我也曾在 .validate 中尝试过这种语法

".validate": "newData.hasChildren(['msg','uid','uname'])"

这也会给我抛出一个权限被拒绝的错误。

这是ios代码

   func sendMessage(text: String!) {
        // *** STEP 3: ADD A MESSAGE TO FIREBASE
        messagesRef.childByAutoId().setValue([
            "msg":text,
            "uname":self.senderDisplayName,
            "uid":self.senderId
            ])
    }

【问题讨论】:

  • 好的,所以我发现我需要添加一个动态节点并且它可以工作,例如$comment_id:{.validation:...}

标签: firebase firebase-security


【解决方案1】:

我只需要将动态消息id添加到json中

{
    "rules": {
      "messages":{
        ".read": "auth.uid !== null",
        ".write": "auth.uid !== null",
       "$message_id":{
           ".validate": "newData.child('msg').exists()",
           "uid": {
             ".validate": "auth.uid === newData.val()"
           }
        }
      }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-23
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 2013-08-05
    相关资源
    最近更新 更多