Jek
Jek

Reputation: 5666

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:

Upvotes: 11

Views: 2365

Answers (1)

Corion
Corion

Reputation: 3925

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

Upvotes: 23

Related Questions