Skip to content

Instantly share code, notes, and snippets.

@thaJeztah
Created November 15, 2014 16:21
Show Gist options
  • Save thaJeztah/ace613f8433ac888ea41 to your computer and use it in GitHub Desktop.
Save thaJeztah/ace613f8433ac888ea41 to your computer and use it in GitHub Desktop.
New regex (JSON API V2)

New regex (JSON API V2)

Quick evaluation of the regex as proposed in moby/moby#9015 and mentioned in moby/moby#8961 (comment)

NOTE 1; The actual regex is [a-z0-9]+(?:[._-][a-z0-9]+)*. I left out the ?:, because that only distracts here and is to make the group 'non-capturing'

NOTE 2; Start (^) and end ($) aren't included in the regex, is this intentional?

###Current Regexp in JSON API V2

                                [a-z0-9]+([._-][a-z0-9]+)*
                                \______/  \___/\_______/
                                    |       |      |
one or more alphanum ---------------+       |      |
                                            |      |
followed by *zero or more*;                 |      |
                                            |      |
                    / separator ------------+      |
                    |                              |
                    | followed by                  |
                    |                              |
                    \ one or more alphanum --------+

This regex makes the second group optional, it will also validate a single alphanum (a).

Both the minimum and maximum length have to be validated separately

###Alternative;

                                [a-z0-9]([._-]?[a-z0-9]+)+
                                \______/ \____/\_______/
                                    |      |      |
one alphanum -----------------------+      |      |
                                           |      |
followed by *at least* one;                |      |
                                           |      |
                   / separator (optional) -+      |
                   |                              |
                   | followed by                  |
                   |                              |
                   \ one or more alphanum --------+

This regex makes the second group required, but the separator optional. Because of this it will only validate if at least two characters are present, or three characters if a separator is used. So;

name result
a invalid (second group missing)
aa valid
a-a valid

Only the maximum length has to be validated separately, because the second group can consist of a variable number of characters, which makes validating it using a regex only complicated (or even not possible).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment