Validation 避免创建额外的儿童Firebase

Validation 避免创建额外的儿童Firebase,validation,firebase,rules,Validation,Firebase,Rules,我是firebase的新手,我想知道如何解决有关hasChildren()RuleDataSnapshot的问题,以及如何验证将创建的数据 db的样本: { "visitors" : { "-KP4BiB4c-7BwHwdsfuK" : { "mail" : "aaa@mail.com", "name" : "aaa", } ..... } 规则: { "rules": { "visitors": {

我是firebase的新手,我想知道如何解决有关hasChildren()RuleDataSnapshot的问题,以及如何验证将创建的数据

db的样本:

 {
  "visitors" : {

   "-KP4BiB4c-7BwHwdsfuK" : {
      "mail" : "aaa@mail.com",
      "name" : "aaa",
    }
    .....
}
规则:

{
    "rules": {
        "visitors": {
            ".read": "auth != null",
            ".write": "auth.uid != null",
                "$unique-id": {
                    ".read": "auth != null ",
                    ".write": "auth != null",
                    ".validate": "newData.hasChildren(['name','mail'])",
            }
        }

    }
}
据我所知,如果要创建数据,数据字段必须具有相同的名称才能通过规则验证。 例如: 如果我按“名称”更改“名称”,并尝试使用其子节点创建一个新节点,则规则将尽我所能工作。 我想知道如果我手动添加一个新字段来创建会发生什么

例如:

//Add extra fields which are not actually present
 var data = {name : "xxx",mail:"xxx@mail.com",extra1:222,extra:333};
 firebase.database().ref('visitors/').push(data);
结果是:

  "visitors" : {
  "-KP4BiB4c-7BwHwdsfuK" : {
      "mail" : "aaa@mail.com",
      "name" : "juan",
     "extra1":222,
      "extra2":333
    }
}
所以我的问题是如何避免在每个节点上创建额外的子节点?我想是规则造成的


提前感谢。

您的验证规则规定您的帖子必须至少有这些子项,而不仅仅是这些子项。 为确保不能添加其他子项,您必须将以下内容添加到规则中:

{
  "rules": {
    "visitors": {
        ".read": "auth != null",
        ".write": "auth.uid != null",
        "$unique-id": {
            ".read": "auth != null ",
            ".write": "auth != null",
            //This line says the new data must have ATLEAST these children
            ".validate": "newData.hasChildren(['name','mail'])",   
            //You can add individual validation for name and mail here     
            "name": { ".validate": true },
            "mail": { ".validate": true },
            //This rule prevents validation of data with more child than defined in the 2 lines above (or more if you specify more children)
            "$other": { ".validate": false }
        }
    }
  }
}
再看一个例子