2

I am looking to apply firestore rules to restrict the change (write, update) of a specific field in a document.

From what I understand in the docs you cannot apply rules on read: to document fields as documents need to be read in their entirety, however, it's not stated about writes, updates?

My structure is like the below example;

match /ads/{adDocument} {

    //adDocument has a field "price" this needs to only be read not changed/updated.

};

How would I go about implementing this?

David Henry
  • 1,972
  • 20
  • 43
  • Please edit the question to be more specific about what exactly you're trying to restrict. Showing code helps. Also note that there are several questions on Stack Overflow about restricting write access to individual fields in Firestore, and maybe a search is all it will take to get what you're looking for. – Doug Stevenson Dec 19 '19 at 05:26
  • If you want to ensure that a field is not modified during a write operation, you can do so with something like `request.resource.data["fieldname"] == resource.data["fieldname"]`. For a more complete example, see https://stackoverflow.com/a/57893473/209103 – Frank van Puffelen Dec 19 '19 at 14:42

1 Answers1

0

You can write a CQRS mediator for all changes, and disallow all write to the documents.

To make a change, the client can add a document to mutate the document on all mutable fields:

/PATCH_ads/{adDocument}:
- itemName: "newItemName"

The mediator should be triggered once a document is added in the PATCH_ads collection. The mediator should reject the change if receiving a command to modify price field, and commit the change if the content of the requested change is valid.

Bill Rao
  • 1
  • 1