gpt4 book ai didi

android - Android 应用程序中的 Firebase 规则验证和捕获验证失败

转载 作者:行者123 更新时间:2023-11-29 01:12:05 25 4
gpt4 key购买 nike

设计

  • 可以登录应用程序的用户
  • 登录用户可以创建客户,这些客户将存储在节点下,其值将是当前登录的用户id

当前数据库值

{
"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"
}
}
}
}

以下是当前为 firebase 中的 customers 表定义的规则。

{
"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":{}
}
}
}
}

尽管我已经为 customerCode 定义了一个规则,即它的值应该少于 4 个字符,但它允许插入 5 个字符。另外,我如何添加一个验证来验证每个 userIdcustomerCode 的唯一性?截至目前它有 !newData.exists() 但它也不起作用。

谁能指导我正确的方向?

最佳答案

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

{
"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"
}
}
}

使用此模型,您可以将规则修改为如下所示:

{
"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",
}
}
}
}

也许上一个示例中的规则并不完美,但我相信您可以通过查看 firechat database rules 找到适合您的应用程序的方式。 .我从那个存储库中学到了很多东西。

关于android - Android 应用程序中的 Firebase 规则验证和捕获验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42268560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com