【问题标题】:Firebase .validate errorFirebase .validate 错误
【发布时间】:2018-06-11 16:45:44
【问题描述】:

我有一个按如下方式组织的 Firebase 数据库。

如您所见,它是一个字典数组(每个字典包含两个条目,'text' 和 'tag')。

它包含以下规则。我想做的是确保 newData 包含一个名为“text”的字符串和一个名为“tag”的字符串。

{
   "rules": {
     ".read": "auth != null",

     ".write": "!data.exists() || !newData.exists()",

       //Can write only Array with text and tag
     ".validate": "newData.hasChildren(['text', 'tag'])",
     "$other": {
        ".validate": false
     },
     "text": {
       ".validate": "newData.isString()"
     },
     "tag": {
       ".validate": "newData.isString()"
     }
   }
 }

当我给它以下 JSON 时,它在第 10 行 (".validate": "newData.hasChildren(['text', 'tag'])",) 上显示“模拟写入被拒绝”。

JSON 文件:

 {
    "3": {
        "text": "text message",
        "tag": "this is a tag"
    }
}

我应该如何编写我的规则? 感谢您的帮助。

【问题讨论】:

    标签: json firebase-realtime-database firebase-security


    【解决方案1】:

    您将验证规则定义得太高了一级。现在您验证整个数据库有子tagtext,但它没有。相反,您想验证根下的每个子节点是否具有这些子节点,这需要您定义一个 $ 变量规则,如下所示:

    {
       "rules": {
         ".read": "auth != null",
         "$childid": {
           ".write": "!data.exists() || !newData.exists()",
    
           //Can write only Array with text and tag
           ".validate": "newData.hasChildren(['text', 'tag'])",
           "$other": {
            ".validate": false
           },
           "text": {
             ".validate": "newData.isString()"
           },
           "tag": {
             ".validate": "newData.isString()"
           }
         }
       }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 2012-02-15
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多