In Cloud Firestore rules - How do I check if a key is null
Reading the list comparisons of the Firestore Security rules documentation here, we can see that
hasAll returns true if all values are present in the list.
// Allow read if one list has all items in the other list allow read: if ['username', 'age'].hasAll(['username', 'age']);
request.resource.data is a map containing the fields and values. In order to use
hasAll, we must first get the keys as a list of values as shown here.
Looking at the docs - https://firebase.google.com/docs/reference/rules/rules.Map
k in x - Check if key k exists in map x
so this should work (without the keys())
!('assignee' in resource.data)
if you want to make sure a key is null you need to check that this key is not part of the resource keys property:
you can also use
hasOnly. more info here
Gal Bracha over 1 year
In Cloud Firestore Rules - I have a document called
taskand I want to see if some data (
assigneefield) is null / don't exists.
resource.data.assignee == null- Does not work (Error)
!resource.data.hasAll(['assignee'])- Does not work (Error)
From the documentation - it states that this indeed creates an error:
// Error, key doesn't exist allow read: if resource.data.nonExistentKey == 'value';
Andrew M. almost 6 yearsShould
!('asignee' in resource.data.keys())work too? Another thing to note is this, from the rules docs: "Fields not provided in the request which exist in the resource are added to
request.resource.data. Rules can test whether a field is modified by comparing
resource.data.fooknowing that every field in the
resourcewill also be present in
request.resourceeven if it was not submitted in the write request.", so there may be times when the key exists and you aren't expecting it to...
dsl101 about 5 years@menehune23 That's true, and it makes it really difficult to tell if the client app has not included a field, in the case where it might already exist in the resource. In addition, the copying of resource to request fields is not honoured by the rules simulator, so you can build an update in the simulator which works, but won't work in a real app... Very frustrating.