本文介绍了Firebase .validate错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按以下方式组织的Firebase数据库.

I have a Firebase database organised as follows.

如您所见,它是一个字典数组(每个字典包含两个条目文本"和标签").

As you can see, it is an array of dictionaries (each dictionary containing two entries, 'text' and 'tag').

它包含以下规则.我希望做的是确保newData包含一个名为"text"的字符串和一个名为"tag"的字符串.

It contains contains the following rules. What I wish to do is make sure that the newData contains a String called 'text' and a String called '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'])",)上说模拟写入被拒绝".

When I give it the following JSON, it says that 'the simulated writing was denied' on line 10 ( ".validate": "newData.hasChildren(['text', 'tag'])", ).

JSON文件:

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

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

How should I write my rules?Thank you for your help.

推荐答案

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

You're defining your validation rule one level too high. Right now you validate that the entire database has children tag and text, which it doesn't. Instead you want to validate that each child node under the root has these children, which requires that you define a $ variable rule like this:

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

这篇关于Firebase .validate错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:44