-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
I think it would be useful to be able to define multiple regex() patterns to a string each with their own, customizable message.
Use case
I am trying to validate a password coming in from users. Let's say, I want to require the password to contain:
- at least one alphabetic character
- at least one number
- minimum of 6 characters
- maximum of 30 characters
Here is a condensed version of my validation schema to make this happen.
// User schema
var schema = Joi.object().keys({
email: Joi.string().trim().email().max(100).required(),
password: Joi.string().min(6).max(30).regex(/.*[0-9].*/).regex(/.*[a-zA-Z].*/)
});The validation works, and the message returned is "password fails to match the required pattern" when trying things that won't pass the two regex validators.
A common best practice is to tell the user what they are missing. For example, if a user tries to enter the password, MyAwesomePassword, I'd like to tell them, they need a number. If they try, 123456, I'd like to tell them they need at least one letter.
Am I just missing something here? Is there already a way to accomplish this with Joi?