joi icon indicating copy to clipboard operation
joi copied to clipboard

Required

Open a00andos opened this issue 11 years ago • 3 comments

I can't get this to work: pass2: Joi.ref('pass').required() But I can require pass2 with .requiredKeys('pass', 'pass2').

Is this a bug or is there a reason for why the first example does not work while the second does?

a00andos avatar Feb 03 '15 15:02 a00andos

Can you put the full object please ?

Marsup avatar Feb 03 '15 16:02 Marsup

schema.keys({
    pass: Joi.string().regex(/^[\S]{8,20}$/).required(),
    pass2: Joi.ref('pass') // I cant add require here. 
}).and('pass', 'pass2'); // Therefore I have to to it like this. 
schema.keys({
    pass: Joi.string().regex(/^[\S]{8,20}$/),
    pass2: Joi.ref('pass')  
}).requiredKeys('pass', 'pass2')  // Or I can do like this. 

a00andos avatar Feb 04 '15 15:02 a00andos

The 1st one is good, you can also write it this way :

var schema = Joi.object().keys({
    pass: Joi.string().regex(/^[\S]{8,20}$/).required(),
    pass2: Joi.valid(Joi.ref('pass')).required()
});

Marsup avatar Feb 04 '15 15:02 Marsup