检查request.resource.data是否是Firebase Firestore中的字符串列表 [英] Check if request.resource.data is a list of strings in Firebase Firestore
问题描述
如何检查Firestore规则中的字段是否为string[]
类型?
我在文档中有一个tags
字段,该字段应该是字符串列表,我想强制执行该操作.通常,如果我想查看该字段是否为int
,我可以说request.resource.data is int
,但是找不到与lists
对应的字段.
谢谢!
规则语言中没有显式类型检查(至少据我所知).因此,我能想到的最好的方法是尝试找到一种方法来区分 List
(数组的类型)以及其他类型.
例如,列表具有join()
方法,该方法可用于将列表中的值连接到单个字符串中.由于(据我所知)其他所有类型都没有该操作,因此此检查可以检测到数组:
allow write: if request.resource.data.categories.join(",") != "";
任何非空数组都将通过此测试,而空数组和其他类型将失败.在模拟器中,这给出了一个非常丑陋的错误消息,但是当暴露给客户端时,这转化为正常的,通用的拒绝权限".
供我将来参考:测试代码位于此处.>
How do I check if a field is of type string[]
in firestore rules?
I have a tags
field in a document, which should be a list of strings, and I want to enforce that. Normally, I can say request.resource.data is int
if I want to see if the field is an int
, but I can't find an equivalent for lists
.
Thanks!
There is no explicit type check in the rules language (at least as far as I know). So the best I can come up with is trying to find a way to distinguish between a List
(the type of an array), and other types.
For example, a list has a join()
method, which can be used to concatenate the values from the list into a single string. Since (as far as I can see) none of the other types have that operation, this check can detect an array:
allow write: if request.resource.data.categories.join(",") != "";
Any non-empty array will pass this test, while empty arrays and other types will fail. In the simulator this gives a pretty ugly error message, but that translates to a normal, generic "permission denied" when exposed to clients.
For my own future reference: test code is here.
这篇关于检查request.resource.data是否是Firebase Firestore中的字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!