使用Firebase规则在Firestore中进行数据验证 [英] Data validations in firestore using firebase rules

查看:34
本文介绍了使用Firebase规则在Firestore中进行数据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经在Firestore规则部分中写了一条规则,例如

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;
    }
  }
}

我要使用 firestore 规则确保新创建的文档中的数量"字段为正.所以我写了一个上面显示的规则,但是没有用.我仍然可以将负值写入 firestore .如何创建规则以确保在 firestore 的数量"字段下没有否定值?

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:

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

这篇关于使用Firebase规则在Firestore中进行数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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