Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hapi route joi validation of password confirmation

How do I check that password and password_confirmation are the same ?

var Joi = require('joi'),
S = Joi.string().required().min(3).max(15);
exports.create = {
   payload: {
            username: S,
            email: Joi.string().email(),
            password: S,
            password_confirmation:  S
   }
}
like image 254
Whisher Avatar asked Apr 23 '15 14:04

Whisher


People also ask

How do you validate a password?

The password must contain one or more uppercase characters. The password must contain one or more lowercase characters. The password must contain one or more numeric values. The password must contain one or more special characters.

What does Hapi Joi do?

Hapi Joi is an object schema description language and validator for JavaScript objects. With Hapi Joi, we create blueprints or schemas for JavaScript objects (an object that stores information) to ensure validation of key information.


1 Answers

If you got "language" is not allowed error message. Oh, you've come to the right place.

Now, 2020 and with Joi v17.2.1 we can use Joi.any().equal() or Joi.any().valid() with Joi.ref() and custom message with messages():

password: Joi.string().min(3).max(15).required().label('Password'),
password_confirmation: Joi.any().equal(Joi.ref('password'))
    .required()
    .label('Confirm password')
    .messages({ 'any.only': '{{#label}} does not match' })

Or use options()

password: Joi.string().min(3).max(15).required().label('Password'),
password_confirmation: Joi.any().equal(Joi.ref('password'))
    .required()
    .label('Confirm password')
    .options({ messages: { 'any.only': '{{#label}} does not match'} })

Validate error will show ValidationError: "Confirm password" does not match if not match.
And show ValidationError: "Confirm password" is required if you have not pass password_confirmation.

Hope useful to someguys.

like image 68
ThangLe Avatar answered Oct 02 '22 14:10

ThangLe