Dean Stamler
Dean Stamler

Reputation: 382

How do you interpolate path in firestore rules functions?

I'm trying to DRY up my rules so I'm writing a function like this

function isInCollection(field, collection) {
  return exists(/databases/$(database)/documents/$(collection)/request.resource.data[field])
}

I've tried at least six permutations of this argument to exists() including using the path() function to construct a path out of a string and I can't get it to properly resolve the path. I'm suspicious that the issue is related to the [] notation around field.

Upvotes: 1

Views: 430

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317412

It's the same syntax as you're already using to interpolate database and collection. Use $() to contain the expression you want to add to the path.

exists(/databases/$(database)/documents/$(collection)/$(request.resource.data[field]))

Upvotes: 2

Related Questions