密码确认和密码相等,但验证仍在React组件中触发JOI [英] Password confirmation and password are equal but validation is still triggering JOI in React component

查看:219
本文介绍了密码确认和密码相等,但验证仍在React组件中触发JOI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试验证密码确认时创建了注册表和问题表.我正在使用最新版本的JOI浏览器.

I creating a register form and the problems accurs while try validating password confirmation. I am using the last version of JOI-Browser.

我尝试了下面的代码,即使密码和密码确认具有相同的值,也触发了验证错误.

I tried the code below and the validation error was triggered even though password and password confirmation have the same values.

password: Joi.string()
      .min(5)
      .required(),
passwordConfirmation: Joi.ref("password")

这是我的状态对象:

password: "12345"
passwordConfirmation: "12345"
username: ""
errors: {…}
passwordConfirmation: "\"passwordConfirmation\" must be one of [ref:password]"

我花了几个小时尝试几种方法并阅读了文档,但还是没有运气,验证仍在触发,

I passed several hours trying several approaches and reading the documentation, but still no luck, the validation is still triggering,

我还有其他形式的验证,它们可以正常工作.

I have other validations in this form and they work fine.

推荐答案

我认为Joi.ref不应该这样使用.

I don't think Joi.ref should be used that way.

我通常会这样做:

const passwordConfirmation = Joi.string()
  .required()
  .valid(Joi.ref('password'))
  .options({
    language: {
      any: {
        allowOnly: '!!Passwords do not match',
      }
    } 
  })

如果您引用文档,您将看到:

If you refer to the docs, you will see:

请注意,引用只能在显式支持的地方使用,例如在valid()或invalid()规则中.如果需要向上(父母)引用,请使用object.assert().

Note that references can only be used where explicitly supported such as in valid() or invalid() rules. If upwards (parents) references are needed, use object.assert().

这篇关于密码确认和密码相等,但验证仍在React组件中触发JOI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆