the way of getting and verifying array list size in Firestore is below
request.resource.data.arraylist.size() < 10
but I want to know the "request.resource.data.arraylist[last_index].length" not the "list size"
how do I figure out this?
The Bytes
, List
, Map
, Set
, and String
variable types in the rules
namespace all share the size()
method which is used in place of length
.
So, using that, you would assert the last element has a size of less than 10 using:
request.resource.data.arraylist[request.resource.data.arraylist.size()-1].size() < 10
Because that looks messy (especially amongst other conditions), you could rewrite it as a custom function like so:
function doesLastElementOfListHaveSizeLessThan(num, list) {
return list[list.size()-1].size() < num;
}
doesLastElementOfListHaveSizeLessThan(10, request.resource.data.arraylist);
Notes:
true
/false
as soon as possible and doesn't throw errors such as the index being out of bounds.