如何检查只有一个特定字段的用户使用Firestore规则进行了更新? [英] How to check if there is only one specific field who is updated with Firestore rules?
本文介绍了如何检查只有一个特定字段的用户使用Firestore规则进行了更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
应用程序端,我有以下代码:
Application side, I have this code :
static func markTaskAsCompleted(field: String, task: Task) -> Promise<Bool> {
return Promise<Bool> { seal in
FirestoreHandler.shared.db.collection("tasks").document(task.id!).updateData([
"\(field)": true
], completion: { (err) in
seal.resolve(err != nil ? false : true, err)
})
}
}
我将更新单个字段,在这种情况下为helper_completed
.这是安全规则:
I update a single field, helper_completed
in this case. Here are the security rules :
match /tasks/{task_id} {
...
allow update: if (...) || isValidTaskUpdate();
...
}
function isValidTaskUpdate() {
return currentUser().uid == existingData().assigned.user_id &&
incomingData().keys().size() == 1 &&
'helper_completed' in incomingData();
}
function incomingData() {
return request.resource.data;
}
最后一个检查'helper_completed' in incomingData()
始终使用先前的应用程序代码返回false.
The last check 'helper_completed' in incomingData()
always return false with the previous application code.
我也尝试过:
'helper_completed' in incomingData().keys();
incomingData().hasAny('helper_completed');
那么,如何检查我的更新请求中是否仅更新了一个特定字段?
So, how can I check if there is only one specific field updated in my update request ?
我已经检查了: Firestore安全规则-如何检查字段是否已被修改?
推荐答案
To check what fields are being updated, use request.writeFields
. In your case this should be:
"helper_completed" in request.writeFields && request.writeFields.size() == 1
这篇关于如何检查只有一个特定字段的用户使用Firestore规则进行了更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文