Firebase, Security Rules, how to check a item of array String size

roun paleum

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?

samthecodingman

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:

  • You can name the function whatever you want, I just used a sentence for clarity and it makes rereading your rules later easier.
  • While this rule asserts the length of the last index, other indexes could have been modified to be longer than your threshold in the same write.
  • For performance reasons, security rules do not support iteration, you must hard code in each check for an index manually. If you need to check a range of indexes, you should implement a custom function that checks a small range of indexes that takes a starting offset. If you implement such a function, make sure that it short-circuits to true/false as soon as possible and doesn't throw errors such as the index being out of bounds.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a way to check the size of a Cloud Firestore collection using Security Rules?

Basic security rules with Firebase

How to validate this data structure with Firebase security rules?

Firebase rule that needs to check for value in array (contains() in security rules)

Firebase security rules with wildcards

Firebase Security Rules for Storage

Firebase Realtime database security rules - check other node

How to check for Duplicates in Security Rules Arrays?

how to check if a filename contains uid in firebase storage security rules?

Firebase security rules like this?

Firebase Storage Security Rules - Property size is undefined on object

Firestore security rules, how to check if claim exists and is a string

Using Firebase Security rules, how can I check for available usernames without making the entire node public?

Firebase Security Rules

Firebase security and rules

firebase - security rules REST

Firebase security rules not working

firebase security rules check 2 fields in data

Firebase security rules confusion

How can I check if a string item within an array is all uppercase?

Firebase check if item is not in array in Flutter

Firestore, Security Rules, how to limit a size of array of map field content

Restrict an array size up to N indices (Security Rules)

Does Firebase App Check discard the need for implementing Security Rules?

How to check the size of a set in drools rules

How to limit (Flutter) string min/max length with security rules in Firebase

Firebase RTDB Security Rules limit overall size of user data?

How to check if there are two (or more) forward slashes in a string of an Array item with Javascript

Firebase security rules that check for the incoming request email