Reputation: 67
I know how to check if a field of an incoming data set is a string or not, but how do I do a conditional check if the field exists, then check if its a string:
For example, "last name" is not a required field, but if it's supplied, then check if its length is greater than 2. I get an error if I just check the length rule generally -> is there a way to check the length only if last_name exists?
Firestore Rule: request.resource.data.last_name.length() > 0
Upvotes: 2
Views: 1379
Reputation: 5182
Function for validating that field would be like this:
function isLastNameValid() {
return request.resource.data.last_name == null || request.resource.data.last_name.size() > 2;
}
Upvotes: 4
Reputation: 317948
Checking if a value is a string is effectively the same as checking for existence. So if you do this:
request.resource.data.last_name is string
It will also return false if it doesn't exist.
But if you don't care what the type is, and you just want to check if it exists at all (as a string, number, whatever):
"last_name" in request.resource.data
request.resource.data is a Map, so check it's documentation at those links.
Upvotes: 2