1

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.

Ibrahim Ali
  • 2,083
  • 2
  • 15
  • 36

1 Answers1

2

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

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84