Reputation: 291
I am looking for the correct Firestore Rules settings that will allow deleting certain document fields.
I need to update the document if the following happens:
DocumentReference documentReference = firebaseFirestore.collection("test").document(questionId);
WriteBatch writeBatch = activityMain.firebaseFirestore.batch();
writeBatch
.update(documentReference, "disable", true)
.update(documentReference, "user_id", FieldValue.delete())
.update(documentReference, "user_name", FieldValue.delete());
writeBatch.commit();
User can only update the document if the field "disable" is set to true, and the fields "user_id" and "user_name" are deleted. Otherwise deny the request.
I have tried already tried this:
allow update: if request.resource.data.disable == true
&& (request.resource.data.size() == resource.data.size() - 2)
&& request.writeFields.size() == 2
&& request.resource.data.user_id in request.writeFields
&& !(request.data.user_id in request.resource.data);
&& request.resource.data.user_name in request.writeFields
&& !(request.data.user_name in request.resource.data);
This apporoach is not working. It always returns false. Any idea how to deal with this?
Upvotes: 1
Views: 65
Reputation: 291
I was able to do it:
allow update: if (request.resource.data.disable == true
&& (request.resource.data.diff(resource.data).removedKeys()
.hasOnly(['user_name', 'user_id'])));
Upvotes: 1