In Cloud Firestore rules - How do I check if a key is null


Solution 1

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']);

The 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.


Solution 2

Looking at the docs -

k in x  - Check if key k exists in map x

so this should work (without the keys())

!('assignee' in 

Solution 3

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 hasAll or hasOnly. more info here

Gal Bracha
Author by

Gal Bracha

Developing for Positive Impact. Helping communities and individuals make a better world. Current Project Genetics and AI Open source: A platform to organize a community in a decentralized way A platform to plan co-created art funds: Fluid dynamics: - Real-time GPU based fluid dynamic system that translates data from a depth sensor /high-speed camera. Other social accounts Facebook Twitter Linkedin Medium My old blog posts

Updated on June 06, 2022


  • Gal Bracha
    Gal Bracha over 1 year

    In Cloud Firestore Rules - I have a document called task and I want to see if some data (assignee field) is null / don't exists.

    I've tried:

    1. == null - Does not work (Error)
    2. !['assignee']) - Does not work (Error)

    From the documentation - it states that this indeed creates an error: // Error, key doesn't exist allow read: if == 'value';

  • Andrew M.
    Andrew M. almost 6 years
    Should !('asignee' in 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 Rules can test whether a field is modified by comparing to knowing that every field in the resource will also be present in request.resource even 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
    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.