Skip to content

Instantly share code, notes, and snippets.

@visualjeff
Created August 3, 2016 18:20
Show Gist options
  • Save visualjeff/b224d0cb1c8e5654ed7fc2b1d5ad19b3 to your computer and use it in GitHub Desktop.
Save visualjeff/b224d0cb1c8e5654ed7fc2b1d5ad19b3 to your computer and use it in GitHub Desktop.
Hapi JOI Regex tips
Regex tips for JOI:
^ asserts that the regular expression must match at the beginning of the subject
[] is a character class - any character that matches inside this expression is allowed
A-Z allows a range of uppercase characters
a-z allows a range of lowercase characters
. matches a period rather than a range of characters
\s matches whitespace (spaces and tabs)
_ matches an underscore
- matches a dash (hyphen); we have it as the last character in the character class so it doesn't get interpreted as being part of a character range. We could also escape it (\-) instead and put it anywhere in the character class, but that's less clear
+ asserts that the preceding expression (in our case, the character class) must match one or more times
$ Finally, this asserts that we're now at the end of the subject
When you're testing regular expressions, you'll likely find a tool like "regexpal" helpful.
principal: Joi.string().regex(/^[a-z0-9]+$/).required(),
credential: Joi.string().regex(/^[a-z0-9]+$/).required()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment