Ibrahim Ali
Ibrahim Ali

Reputation: 2503

Firestore security rules | enforcing field VALUES

It is possible to enforce field types with firestore security rules but is it possible to enforce field VALUE?

Lets say when a message is seen by the user.. The message seen field gets updated to true. And because this can never go back to false. How do I write a rule to only update seen field if it is to be updated to true.

Upvotes: 1

Views: 216

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

The following rules should:

  • allow creating a message document with seen value as false
  • update seen to true only that too it is currently false
 match /messages/{messageId} {
   allow create: if request.resource.data.seen == false;
   allow update: if resource.data.seen == false && request.resource.data.seen == true;
 }

Do update the rules as per other requirements e.g. user must be authenticated and so on.

resource.data contains data of document being updated and request.resource.data contains data to be added/updated in the document. More details about this can be found in the documentation

Upvotes: 2

Related Questions