Skip to content

Custom messages and multiple regex restrictions. #439

@jarrodpayne

Description

@jarrodpayne

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:

  1. at least one alphabetic character
  2. at least one number
  3. minimum of 6 characters
  4. 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions