1

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?

Miha M
  • 379
  • 1
  • 3
  • 14
  • 2
    writeFields hasn't been supported for a long time now. No new code should use it. Read this: https://stackoverflow.com/a/52192476 – Doug Stevenson Sep 09 '22 at 20:16

1 Answers1

1

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'])));
Miha M
  • 379
  • 1
  • 3
  • 14