hapi route joi validation of password confirmation

Whisher picture Whisher · Apr 23, 2015 · Viewed 11.2k times · Source

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
   }
}

Answer

Gergo Erdosi picture Gergo Erdosi · Apr 23, 2015

You can use Joi.any().valid() with Joi.ref():

password: Joi.string().min(3).max(15).required(),
password_confirmation: Joi.any().valid(Joi.ref('password')).required().options({ language: { any: { allowOnly: 'must match password' } } })