rahulserver
rahulserver

Reputation: 11205

Firestore rules unable to check if a reference exists

Here are my firestore rules:

match /databases/{database}/documents {
    match /customers/{customerId} {
        allow write: if  exists(/databases/$(database)/documents/$(request.resource.data.referred_by));
        allow read: if true;
    }
}

The following data does not pass this rule(I was able to create the data from client app by removing the exists(/databases/$(database)/documents/$(request.resource.data.referred_by)):

enter image description here

So how do we deal with the reference field so that I can validate whether this reference exists already or not?

EDIT:

The type of the referred_by field is reference. enter image description here

Upvotes: 1

Views: 80

Answers (1)

rahulserver
rahulserver

Reputation: 11205

It seems that its a limitation of firestore at this stage. We are not able to validate whether a reference exists or not.

I further did some digging using the rules playground. The request.resource.data.referred_by has a path child. So I used $(request.resource.data.referred_by.path). Still that did not work because the $(request.resource.data.referred_by.path) resolves to customers%2FB85CSuRJQZWUKt2jgxOy (url-encoded).

enter image description here I don't know of a way to url decode this in firestore rules. So at this time I am changing my data models to use referred_by as a 'string' instead of a reference.

I hope this thing is resolved in future by firestore team.

Upvotes: 1

Related Questions