在Firebase数据库中使用多个验证规则 [英] Usage of multiple Validation rules in Firebase Database

查看:262
本文介绍了在Firebase数据库中使用多个验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不清楚Firebase数据库验证规则的几个方面,并且已经阅读文档几次我的具体情况/问题如下:



我的数据方案是:

  {
aNumber:0.794180524700695,
clicks:11
}

我当前的验证规则是:

  {
rules:{
.validate:newData.child('aNumber')。isNumber(),
.read:true,
.write:true
}
}

允许写入aNumber,如果它是一个数字,但我还要验证字段点击是一个数字。



我尝试使用和运算符(&&):

  {
rules:{
.validate:newData.child('aNumber')。isNumber()&
newData.child('clicks') .isNumber(),
.read:true,
.write:true
}
}
pre>

但是当我只更新一个字段时失败:

  {
aNumber:2
}



感谢

解决方案

div>

所以经过一些实验后,我发现了自己的问题的答案:



我的新规则看起来像这样:

  {
rules:{
.read:true,
.write:true,
aNumber:{
.validate:newData.isNumber()
},
clicks:{
.validate:newData.isNumber )
}
}
}

对于全局的特定子节点,验证发生在规则中的特定节点级别,这对于一些可能是显而易见的,但是它需要我一点实现。


I am unclear on a couple of aspects of Firebase Database validation rules, and have already read the documentation a couple of times my specific case/questions are as follow:

My data scheme is this:

{
  "aNumber" : 0.794180524700695,
  "clicks" : 11
}

My current validation rules are this:

{
  "rules": {
    ".validate": "newData.child('aNumber').isNumber()",
     ".read": true,
    ".write": true      
  }
}

This rule currently allows the write for "aNumber" if it is a number, but I would like to also validate for the field "clicks" to be a number.

I tried using the and operator (&&):

{
  "rules": {
   ".validate": "newData.child('aNumber').isNumber() && 
                 newData.child('clicks').isNumber()",
     ".read": true,
    ".write": true      
  }
}

But it fails when I only update one field:

{
 "aNumber": 2
}

How would I validate for this ? , what is a good way of validating multiple independent fields in general ?

Thanks

解决方案

So after some experimenting I found an answer to my own question:

My new rules look like this:

{
 "rules": {
  ".read": true,
  ".write": true,
  "aNumber": {
      ".validate": "newData.isNumber()"
     },
   "clicks": {
      ".validate": "newData.isNumber()"
     }                
 }
}

So instead of validating for a specific child node globally, the validation happens at the specific node level in the rules, this might be obvious to some, but it took me a bit to realize.

这篇关于在Firebase数据库中使用多个验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆