Search code examples
google-cloud-firestorefirebase-security

Data validating in firestore security rules, refuse if unknown data


match /my-data/{userId}
{
  allow write: if "name" in request.resource.data;
}

This above validates if name is in the data. But it also allows any other data to be written along with "name". What do I do if I only want name in the data, and if any other data is provided, then deny write operation?


Solution

  • You're looking for the List.hasOnly operation, which allows you to ensure a list has only certain members.

    Something like this:

    request.resource.data.keys().hasOnly("name")
    

    If the document can already contain any fields, but only the name field is allowed to be modified, you'll want to combine hasOnly wih the map.diff() operation.

    So that'd be something like this:

    resource.data.diff(request.resource.data).keys().hasOnly("name")