Number of update fields in firestore security rules

我的梦境 提交于 2020-05-15 10:55:08

问题


I am making batch operation from client to update only one field but while making batch operation and on testing in security rules It is observed that more than one fields are being updated. I checked this using request.resource.data.size() >1 and request.resource.data.keys().size()>1 returning true(document being updated) but this is not intended as I want to check in security rules that only one field is being updated using checks like request.resource.data.keys().hasOnly(['someFieldToUpdate']) but this is not working now, previously I remember there was writeFields to check that but it is not present now in documentation and also this answer mentions it. So how can I check the fields which are actually being updated in batch operations now ?


回答1:


The request.resource.data field contains the resource as it will exist after the write operation has succeeded. The writeFields property was removed since it could not always reliably be populated.

Right now the only option I can think of is to check if each individual field has changed, and then only allow it if there's one change. But to be honest, that sounds like an odd use-case. A more common use-case I see it to limit what specific fields the user can update, not how many they can update at once.




回答2:


You can use the new diff() method on the data map. It tells you what keys are changed. Here is a sample:

function isUpdateToOpenField(attr) {
    return request.resource.data.diff(resource.data).changedKeys().hasOnly(['open']);
}

allow update: if isUpdateToOpenField(request.resource.data);

Adapted from this answer.



来源:https://stackoverflow.com/questions/54326722/number-of-update-fields-in-firestore-security-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!