How to check if there is only one specific field w

2019-08-28 06:51发布

问题:

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)
        })
    }
}

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;
}

The last check 'helper_completed' in incomingData() always return false with the previous application code.

I have also tried :

 '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 ?

I have already checked : Firestore Security Rules - How can I check that a field is/isn't being modified?

回答1:

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