如何将下面的通用函数用于嵌套字段?我在下面有这份位于 /userdata/{uid}
的文档:
正如所见,userBirthTime
是一张地图,它包含另一个名为 birthTime
的地图,而该地图又包含我想要为其类型进行验证的字段,例如检查传入的 year
是否为 int
类型,然后才允许将其存储在 Firestore 中。
我该怎么做?嵌套字段甚至可能吗?我试过
function isInteger(fieldName) { return request.resource.data[fieldName] is int }
然后就这样用了
isInteger('userBirthTime.birthTime.year')
总体安全规则是这样
match /userdata/{uid} {
allow write: if isInteger('userBirthTime.birthTime.year')
}
但是即使我尝试使用 true
作为 string
而不是所需的 fieldName
类型,它也总是返回 int
?!
我做错了什么?是嵌套字段而不是顶级字段吗?
答案 0 :(得分:1)
显然点符号在安全规则中不起作用(尽管它总是返回 false):
// the request body
"data": {
"userBirthTime": {
"birthTime": {
"year": "string"
}
}
}
改变规则对我有用:
function isInteger(fieldName) {
return request.resource.data.userBirthTime.birthTime[fieldName] is int
}
match /userdata/{uid} {
allow write: if isInteger('year'); // other fields
}