Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reg expression for Firestore constraints on document IDs?

Firestore has constraints on IDs (https://firebase.google.com/docs/firestore/quotas)

What is the Javascript regex for checking the constraints on the following:

  • Must be valid UTF-8 characters
  • Must be no longer than 1,500 bytes
  • Cannot contain a forward slash (/)
  • Cannot solely consist of a single period (.) or double periods (..)
  • Cannot match the regular expression __.*__
like image 516
Jek Avatar asked Oct 17 '18 08:10

Jek


1 Answers

Let's look at these points:

Must be valid UTF-8 characters

I would assume that this is more an issue of your programing language of choice, at least until you tell us that you have raw octets and want a regular expression to validate that a sequence of raw octets is a valid UTF-8 sequence.

Must be no longer than 1,500 bytes

This will mean something like .{1,1500}

Cannot contain a forward slash

This will mean something like [^/]{1,1500} instead of .{1,1500}.

Cannot solely consist of a single period or double periods.

This means something like (?!\.\.?) .

Cannot match the regular expression __.*__

This means something like (?!__.*__) . Maybe it should mean that no ID is allowed to start with __ and to end with __ , but maybe it means that no ID is allowed to contain a substring that starts/ends with __. My approach plays it safe and rejects anything that contains the substring.

Combining the above we get something like:

^(?!\.\.?$)(?!.*__.*__)([^/]{1,1500})$

Shortening the maximum length to something sensible like 10, some test cases:

Accept
foo
foo.
foo..
Reject
bar/
12345678901
foo__bar__
.
..

Fiddle

like image 181
Corion Avatar answered Nov 11 '22 22:11

Corion