Problem validating with Joi


I’m struggling a little bit with Joi validation - specifically how to validate the password confirmation field.

In the documentation example schema it gives the password confirmation as:

repeat_password: Joi.ref('password'),

Yet that only works if the referenced property, password, passes its validation.

Is there any way to have repeat_password (or in my case confirmPassword) be required() and be equal to password?

I’ve tried the following from stackoverflow, with no luck:

  // confirmPassword: Joi.any().valid(Joi.ref('password')).required()
  // confirmPassword: Joi.string().required().equal(Joi.ref('password'))
  // confirmPassword: Joi.string().required().valid(Joi.ref('password'))
  // confirmPassword: Joi.any().valid(Joi.ref('password')).required().options({ language: { any: { allowOnly: 'must match password' } } })

Codepen example here (type a password to see the validation actually work)

I’m also struggling to get the messages function to work. It just straight up says it isn’t a function when I try to chain it.

It’s probably just me, but is Joi… a bit rubbish? It’s making me really like express-validator right now, and that’s a massive achievement!

Would really appreciate some help if anyone has had difficulties with this before!



In case this helps anyone in the future - the problem is that I was using joi-browser (rather than joi) in codepen (which seems to be the only CDN that I can find). Using their sandbox provides the correct result with the above code, and I imagine with the examples that also follow it.

It also fixed another problem with custom messages that wasn’t working in the codepen demo too.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.