在Android应用程序中,Firebase规则验证和捕获验证失败 [英] Firebase Rules validation and catching validation fail in android application

查看:207
本文介绍了在Android应用程序中,Firebase规则验证和捕获验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  • 可以登录应用程序的用户

  • 登录用户可以创建存储在节点中的客户,其值将是当前登录的用户标识符



当前DB值

  {
customers :{
UserId1:{
custId1:{$ b $customerCode:thk,
customerLimit:58866,
customerName:Test New
},
custId2:{
customerCode:thh,
customerLimit:5698,
customerName:Yeth
}
},
UserId2:{
custId3:{$ b $customerCode:thh,
customerLimit:5886,
customerName:Test
},
custId4:{$ b $customerCode:tbh,
customerLimit:58669,
custome rName:New Test



$ b $ $ $

$ b

以下是目前为客户中为 firebase 中定义的规则。


$ b $

  {
rules:{
customers:{
.read:auth!= null,
.write:auth!= null,
$ CID:{
customerName:{$ b $.validate:newData。 isString()&& newData.val()。length< 100
},
customerCode:{
.validate:newData.isString()&& newData.val()。length< 4&& !newData.exists()

customerLimit:{}
}
}
}
}


$ b即使我已经定义了一个 customerCode 的规则,即它应该有值小于4个字符,它允许插入5个字符。我怎样才能添加一个验证验证 customerCode 为每个 userId ?截至目前它有!newData.exists(),但它不能工作。



任何人都可以请指导我在正确的方向?

解决方案

我认为你可以插入最多5个字符导致你的 newData.val()。lenght 从0开始,就像数组 .length code>。换句话说,如果你想为每个userId链接一个customerCode的唯一性,你应该在你的firebase数据库中创建一个新表,如:

  {
customers:{
U serId1:{
custId1:{$ b $customerCode:customerId1,
customerLimit:58866,
customerName:Test New
},
custId2:{
customerCode:customerId2,
customerLimit:5698,
customerName:Yeth$
UserId2:{
custId3:{
customerCode:customerId3,
customerLimit:5886 ,
customerName:Test
},
custId4:{
customerCode:customerId4,$ b $customerLimit:58669 ,
customerName:新测试
}
}
}
customerCode:{$ b $custId1:{
customerId1:thh
}
custId2:{
customerId2:thk
}
custId3:{
customerId3:thh
}
custId4:{
customerId4:tbh
}
}
}

使用这个模型,您可以修改您的规则,如下所示:


$ b $

  {
rules:{
customers:{
.read:auth!= null,
.write:auth!= null,
$ CID:{
customerName:{$ b $.validate:newData。 isString()&& newData.val()。length< 100

customerCode:{
//代码应该是字符串,少于4个字符,并且不能存储在customerCode表中当前userId
.validate:newData.isString()&& newData.val()。length< 3&& ()

customerLimit:{}
}
}
customerCode:{
$ CID:{
CustomerCodeId:{
.read:auth!= null,
.write: auth!= null,
}
}
}
}

最后一个例子中的规则可能并不完美,但我相信你可以找到关于它如何适应你的应用程序的方式,来看看 firechat数据库规则,我学到了很多东西,希望它能帮助你!

b $ b

Design

  • A user who can login to the application
  • Logged in user can create customers which will be stored under node whose value will be the current logged in userid

Current DB values

{
  "customers" : {
    "UserId1" : {
      "custId1" : {
        "customerCode" : "thk",
        "customerLimit" : "58866",
        "customerName" : "Test New "
      },
      "custId2" : {
        "customerCode" : "thh",
        "customerLimit" : "5698",
        "customerName" : "Yeth"
      }
    },
    "UserId2" : {
      "custId3" : {
        "customerCode" : "thh",
        "customerLimit" : "5886",
        "customerName" : "Test "
      },
      "custId4" : {
        "customerCode" : "tbh",
        "customerLimit" : "58669",
        "customerName" : "New Test"
      }
    }
  }
}

Below is the rule currently defined for customers table in firebase.

{
  "rules": {
    "customers":{
      ".read": "auth != null", 
      ".write": "auth != null", 
      "$CID":{
            "customerName":{
                ".validate": "newData.isString() && newData.val().length < 100"
            },
            "customerCode":{
                ".validate": "newData.isString() && newData.val().length<4 && !newData.exists()"
            },
            "customerLimit":{}
      }
    }   
  }
}

Even though I've defined that a rule for customerCode i.e. it should have value less than 4 characters, it is allowing to insert 5 characters. Also how can I add a validation which validates uniqueness of customerCode for each userId? as of now it has !newData.exists() but its not working too.

Could anyone please guide me in the right direction?

解决方案

I think that you can insert up to 5 characters cause your .length for your newData.val().lenght start at 0, like an array. In other way, if you want to link uniqueness of a customerCode for each userId you should create a new table in your firebase database, Something like:

{
  "customers": {
    "UserId1": {
      "custId1": {
        "customerCode": "customerId1",
        "customerLimit": "58866",
        "customerName": "Test New "
      },
      "custId2": {
        "customerCode": "customerId2",
        "customerLimit": "5698",
        "customerName": "Yeth"
      }
    },
    "UserId2": {
      "custId3": {
        "customerCode": "customerId3",
        "customerLimit": "5886",
        "customerName": "Test "
      },
      "custId4": {
        "customerCode": "customerId4",
        "customerLimit": "58669",
        "customerName": "New Test"
      }
    }
  }
  "customerCode": {
    "custId1": {
      "customerId1": "thh"
    }
    "custId2": {
      "customerId2": "thk"
    }
    "custId3": {
      "customerId3": "thh"
    }
    "custId4": {
      "customerId4": "tbh"
    }
  }
}

Using this model, you can modify your rules to so something like this:

{
  "rules": {
    "customers": {
      ".read": "auth != null",
      ".write": "auth != null",
      "$CID": {
        "customerName": {
          ".validate": "newData.isString() && newData.val().length < 100"
        },
        "customerCode": {
          //Code shouls be string, less than 4 characters and it can't be already stored on customerCode table for the current userId
          ".validate": "newData.isString() && newData.val().length<3  && !root.child('customerCode').child(auth.uid).exists()"
        },
        "customerLimit": {}
      }
    }
    "customerCode": {
      "$CID": {
        "CustomerCodeId": {
          ".read": "auth != null",
          ".write": "auth != null",
        }
      }
    }
  }

Maybe the rules are not perfect in the last example, but I'm sure that you can find the way about how it can fits in your app taking a look about firechat database rules. I learned a lot looking that repository. Hope that it will help you!

这篇关于在Android应用程序中,Firebase规则验证和捕获验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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