javascriptregexjsonschemauritemplate

RegEx for URI Templates (RFC 6570) wanted


I need to validate a string to ensure that it is a valid URI Template according to RFC 6570. This should be quite easy with a regular expression. However, I did not find one – is there really no RegEx for URI Templates (yet)?

Context: I would like to use it in a JSON Schema that is processed with node.js.


Solution

  • A little late to the party here, but since the only answer doesn't address the issue of a regular expression for URI templates, I decided to take a stab at it as I couldn't find one either. This is based solely on the ABNF rules in Section 2 of RFC 6570 and I included some ASCII art to map out the different components.

    ^([^\x00-\x20\x7f"'%<>\\^`{|}]|%[0-9A-Fa-f]{2}|{[+#./;?&=,!@|]?((\w|%[0-9A-Fa-f]{2})(\.?(\w|%[0-9A-Fa-f]{2}))*(:[1-9]\d{0,3}|\*)?)(,((\w|%[0-9A-Fa-f]{2})(\.?(\w|%[0-9A-Fa-f]{2}))*(:[1-9]\d{0,3}|\*)?))*})*$
      \                            \_____________/ \\____________/ \\__________________/    \__________________/ /\________________/ /  \_________________________________________________________________/ //
       \                            pct-encoded /   \   operator    \\     varchar                varchar       /  modifier-level4  /                               varspec                                //
        \______________________________________/     \               \\________________________________________/                   /                                                                      //
                        literals                      \               \                varname                                    /                                                                      //
                                                       \               \_________________________________________________________/                                                                      //
                                                        \               \                        varspec                                                                                               //
                                                         \               \____________________________________________________________________________________________________________________________//
                                                          \                                                                   variable-list                                                           /
                                                           \_________________________________________________________________________________________________________________________________________/
                                                                                                                       expression
    

    You can check it out on regex101.com. This should work in JSON Schema since I'm using the ECMA syntax. There might also be some simplifications that can be made, but as I said, this aligns with the ABNF, so it should be relatively easy to follow, if you're familiar with the specification.