My collection path is
{Country}/{State}/{ZipCode}
where I'm storing some data in firestore as shown in the image pasted.
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
?
Try changing the document path to:
one should check if the field even exists
before checking for it's integer value
while the operations are
create
(andupdate
):resulting in a rule alike this:
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:
HTML page: