Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Data From Request Path in Firestore Rules

I am trying to figure out how to get an array from the path on an update or create in order to create validators based on the path data:

function test() {

  // option 1
  return string(request.path).split('/')[0] == 'databases';

  // option 2
  // return string(request.resource.__name__).split('/')[0] == 'databases';
}

match /posts/{document} {
  allow update: if test();
  ...
}

I have tried both of the previous examples with request.resource.__name__ and request.path... How do I parse the data out of the requested path?

Thanks,

J

like image 920
Jonathan Avatar asked Feb 10 '26 17:02

Jonathan


1 Answers

Lets assume this is my request.path /databases/%28default%29/documents/posts/123456.

function test(docId) {
  // request.path supports map access.
  return request.path[4] == '123456'; // this will return true;
  return request.path[3] == 'posts'; // this will return true;
  return docId == '123456'; // this will return true;
}
match /posts/{documentId} {
  allow update: if test(documentId);
}

So if your path has 5 segments, request.path[4] && request.path[3] will return the last 2 segments.

like image 97
Peter O. Avatar answered Feb 12 '26 16:02

Peter O.