【问题标题】:Data validations in firestore using firebase rules使用 Firebase 规则在 Firestore 中进行数据验证
【发布时间】:2019-02-27 08:16:04
【问题描述】:

我的收藏路径是 {Country}/{State}/{ZipCode} 我将一些数据存储在 Firestore 中,如粘贴的图像所示。

我在 firestore 规则部分写了一条规则,比如

 service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {   
            allow read;
       allow write: if request.resource.data.Quantity>0;
    }
  }
}

我想使用firestore 规则确保新创建的文档中的数量字段为正。所以我写了一条上面显示的规则,但它不起作用。我仍然可以将负值写入firestore。 如何创建规则以确保firestore 的数量字段下没有负值?

【问题讨论】:

  • 实际上你的规则确实有效。也许你在修改它们之后没有等待足够长的时间来尝试它。
  • firebase 模拟器中有一个模拟器可以检查更改是否有效。根据 Firebase,即使发布规则也只需要一分钟左右
  • 看我的回答。你可以试试我提供的 HTML 页面:在我这边它可以正常工作。

标签: firebase google-cloud-firestore


【解决方案1】:
  • 应该检查该字段是否存在

  • 在检查它的整数值之前

  • 虽然操作是create(和update):

产生类似这样的规则:

service cloud.firestore {
  match /databases/{database}/documents {

    // match /{document=**} {
    // match /Orders/{document=**} { 
    match /{Country}/{State}/{PostCode}/{document=**} {  
      allow read;
      allow create: if request.resource.data.Quantity != null && int(request.resource.data.Quantity) > 0
      allow update: if request.resource.data.Quantity != null && int(request.resource.data.Quantity) > -1
    }

  }
}

【讨论】:

  • Firestore 是一个 NoSql 数据库,如果字段不存在,它会创建字段。它是一个基于 JSON 的数据库
  • 存储数据的路径清楚地反映在所附图像中。请尝试使用其他答案中给出的规则编写 Firestore。然后,您可以确定您的答案是错误的。感谢您的帮助顺便说一句:)
  • 但是你的回答没有改变文档路径,我试过了,还是不行。
  • @SushantSomani 为createupdate 添加了完整路径和数据验证规则...另一个答案也没有使用完整路径 - 但它仍然有效,因为子集合是由** 通配符匹配。而从实际的角度来看,应该只使用create,否则无法将值更新为0
  • 甚至对其进行了改进,因此只能使用> -1 创建值> 0 而不能更新为负值(为了允许0,例如,以防客户这样做否则决定)...使用OrderId 作为文档名称也可能有意义-PostCode 已经指示State (这使得这个冗余数据)...我的意思是,match /Orders/{document=**} 会根据提供的示例数据,为结构提供更多逻辑意义。
【解决方案2】:

尝试将文档路径更改为:

service cloud.firestore {
match /databases/{database}/documents {
match /{Country}/{State}/{document=**} {   
allow read;       
allow write: if request.resource.data.Quantity > 0;
}
}
}

【讨论】:

  • 仅供参考,实际上是@SushantSomani 在问题 do work 中显示的规则。也许他在修改它们之后没有等待足够长的时间才尝试。
  • @RenaudTarnec 在 firebase 模拟器中有一个模拟器来检查更改是否有效。根据火力基地,即使发布规则也只需要一分钟左右
  • @SushantSomani 我没有尝试使用模拟器,而是尝试使用 0、2 和 -1 等数量值写入 Firestore 的完整 HTML 页面
【解决方案3】:

如果我没记错的话,你的规则确实有效。

我已经用一个 HTML 页面尝试了你的规则,该页面试图编写具有不同 Quantity 值的文档,请参见下面的代码。在这两种情况下(简单集合或子集合),只有文档 A 和 D 被写入数据库(文档 B 和 C 生成错误)。

规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write: if request.resource.data.Quantity > 0
    }
  }
}

HTML 页面:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="https://www.gstatic.com/firebasejs/5.4.1/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.4.1/firebase-firestore.js"></script>
</head>

<body>
    <script>
        // Initialize Firebase
        var config = {
            apiKey: ".....",
            authDomain: ".....",
            databaseURL: ".....",
            projectId: "....."
        };

        var db = firebase.firestore();
        var theRef = db.collection('India').doc('Maharashtra').collection('411057');
        theRef.doc("A").set({
            name: "Mumbai",
            Quantity: 1
        });

        theRef.doc("B").set({
            name: "Mumbai",
            Quantity: 0
        });

        theRef.doc("C").set({
            name: "Mumbai",
            Quantity: -2
        });

        theRef.doc("D").set({
            name: "Mumbai",
            Quantity: 2
        });


        var theRef = db.collection('India1');
        theRef.doc("A").set({
            name: "Mumbai",
            Quantity: 1
        });

        theRef.doc("B").set({
            name: "Mumbai",
            Quantity: 0
        });

        theRef.doc("C").set({
            name: "Mumbai",
            Quantity: -2
        });

        theRef.doc("D").set({
            name: "Mumbai",
            Quantity: 2
        });

    </script>

</body>
</html>

【讨论】:

    猜你喜欢
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2017-04-28
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多