hapi route joi validation of password confirmation

后端 未结 2 1342
野性不改
野性不改 2021-01-02 08:04

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          


        
2条回答
  •  生来不讨喜
    2021-01-02 08:55

    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.

提交回复
热议问题