Ilja
Ilja

Reputation: 46537

Using regular expressions in firestore security rules (testing for spaces in string)

I am reading through documentation and found that firestore security rules expose .matches() helper for regex related operations.

Does something similar exist to test agains regular expresion, i.e. I need to check if request.resource.data.username does not have spaces in it.

In javascript land it would go something like

!/\s/.test(request.resource.data.username)

Upvotes: 3

Views: 913

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

A pattern that match any character but a whitespace is \S. To match any 0 or more occurrences, apply * after it, or if you want to match 1 or more occurrences use + quantifier. To match the start of string use ^ (only works as start of string when used at the start of the Firebase pattern), to match the end of string use $ (only works as end of string anchor when at the end of the pattern).

So, use

.matches(/^\\S+$/)

or

.matches(/^\\S*$/)

Upvotes: 2

Related Questions