简体   繁体   English

用于数据验证的Firebase规则

[英]Firebase Rule for Data Validation

Assuming a data structure like below, I want a rule that disallows adding new car makes, for example users cannot add "toyota". 假设数据结构如下所示,我想要一个禁止添加新车品牌的规则,例如用户不能添加“丰田”。 So, I need to .validate such that the .update must match an existing make. 因此,我需要进行.validate验证,以使.update必须与现有的make匹配。

I have tried using $cars with ".validate": "newData.val().contains($cars)" and all kind of other ways with no luck. 我尝试将$ cars与“ .validate”:“ newData.val()。contains($ cars)”结合使用,并且使用其他各种方式也没有运气。 Any help is appreciated. 任何帮助表示赞赏。

My code is: 我的代码是:

function setModel(model,make,key){
    if (model && make && key){
        var carsRef = ref.child('cars');
        var makeRef = carsRef.child(make);
        var modelRef = makeRef.child(model);
        var obj = {};
        obj[key] = true;           
        modelRef.update(obj, onComplete);
    }
}

The firebase looks like: 火力基地看起来像:

{
"cars" : {
   "chevrolet" : {
      "silverado" : {
         "id192874623" : true,
         "id786766663" : true
      }
   },
   "ford" : {
      "taurus" : {
         "id736273627" : true
      }
   },
   "honda" : {
      "accord" : {
         "id635263535" : true
      }
   }
}}

To disallow adding new car brands: 禁止添加新的汽车品牌:

"cars": {
  "$brand": {
    ".validate": "data.exists()"
  }
}

This is covered in the section on existing data vs new data of the documentation. 在文档的现有数据与新数据部分中对此进行了介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM