hapi route joi validation of password confirmation

2020-07-08 08:01发布

问题:

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

回答1:

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