Is there a way to do this using Joi?
For example:
Joi.string()
.required()
.min(8)
.max(16)
.pattern(/(?=(?:.*[a-z]){2,16}).+/)
.pattern(/(?=(?:.*[A-Z]){2,16}).+/)
.pattern(/(?=(?:.*[0-9]){2,16}).+/)
.pattern(/(?=(?:.*[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]){2,16}).+/)
.messages({
'string.base': 'Username must be of type string',
'string.regex': 'Username must contain at least two lower-case letters',
'string.regex2': 'Username must contain at least two upper-case letters',
'string.regex3': 'Username must contain at least two numbers',
'string.regex4': 'Username must contain at least two special characters',
'string.min': 'Username must be at least 8 characters long',
'string.max': 'Username must be no more than 16 characters long',
'string.empty': 'Username is a required field',
}),
})
});
The only problem I seem to be running into is getting back a good error message for the regular expressions. What should the keys be for messages? I would also like to see all failed validations, not just the first one that fails? Is there a better approach to this?
Then to get all messages: