Data validations in firestore using firebase rules

2019-08-07 14:54发布

My collection path is {Country}/{State}/{ZipCode} where I'm storing some data in firestore as shown in the image pasted. enter image description here

I've written a rule in firestore rules section like

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

I want to make sure that the Quantity field in newly created document is positive using firestore rules. So I've written a rule the one shown above but it is not working. I can still write negative values to the firestore. How can I create a rule to ensure that there are no negatives values under Quantity field of firestore?

3条回答
Melony?
2楼-- · 2019-08-07 15:19

Try changing the document path to:

service cloud.firestore {
match /databases/{database}/documents {
match /{Country}/{State}/{document=**} {   
allow read;       
allow write: if request.resource.data.Quantity > 0;
}
}
}
查看更多
闹够了就滚
3楼-- · 2019-08-07 15:20
  • one should check if the field even exists

  • before checking for it's integer value

  • while the operations are create (and update):

resulting in a rule alike this:

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
    }

  }
}
查看更多
forever°为你锁心
4楼-- · 2019-08-07 15:37

If I am not mistaking, your rules do work.

I have tried your rules with an HTML Page that tries to write documents with different Quantity values, see the code below. In the two cases (simple collection or sub-collection) only docs A and D are written to the database (and docs B and C generate an error).

Rules:

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

HTML page:

<!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>
查看更多
登录 后发表回答