避免创建额外的孩子 Firebase
Avoid create extra childs Firebase
我是 firebase 的新手,我想知道如何解决关于 hasChildren() RuleDataSnapshot 的问题以及如何验证数据的创建。
数据库样本:
{
"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'])",
}
}
}
}
据我所知,如果要创建数据,数据字段必须具有相同的名称才能通过规则验证。
例如 :
如果我根据 "names" 更改 "name" 并且我尝试用他们的孩子创建一个新节点,据我所知,该规则有效。
我想知道 ¿ 如果我手动添加要创建的新字段会发生什么?
例如:
//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
}
}
所以我的问题是如何避免为每个节点创建额外的子节点?我以为规则做到了。
提前致谢。
你的验证规则说你的 post 必须有 atleast 那些 children 而不是 only 那些 children。
为确保无法添加其他 children,您必须将以下内容添加到您的规则中:
{
"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 }
}
}
}
}
看看here另一个例子。
我是 firebase 的新手,我想知道如何解决关于 hasChildren() RuleDataSnapshot 的问题以及如何验证数据的创建。
数据库样本:
{
"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'])",
}
}
}
}
据我所知,如果要创建数据,数据字段必须具有相同的名称才能通过规则验证。 例如 : 如果我根据 "names" 更改 "name" 并且我尝试用他们的孩子创建一个新节点,据我所知,该规则有效。 我想知道 ¿ 如果我手动添加要创建的新字段会发生什么?
例如:
//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
}
}
所以我的问题是如何避免为每个节点创建额外的子节点?我以为规则做到了。
提前致谢。
你的验证规则说你的 post 必须有 atleast 那些 children 而不是 only 那些 children。 为确保无法添加其他 children,您必须将以下内容添加到您的规则中:
{
"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 }
}
}
}
}
看看here另一个例子。