Firebase Firestore Security Rule: touching resource.data in create rule always returns false

Solution 1:

Well it turns out that the reason why if ('foo' in resource.data) || !('foo' in resource.data); doesn't evaluate to true is because resource.data is silently throwing an exception on create, and it forces the whole thing to evaluate to false. The way to check whether the rule is triggered by a create or update in a write rule is as simple as this:

resource == null // if true, it's create, otherwise, it's update

2022-01-22 Update:

There's a more semantic way to determine whether the operation was a create:

request.method == 'create'

Docs: https://firebase.google.com/docs/rules/rules-language#building_conditions