【问题标题】:Cyclic dependency, node was:"password" in yup循环依赖,节点是:是的“密码”
【发布时间】:2021-09-11 09:27:51
【问题描述】:

我收到了上述错误。不知道对象发生了什么。下面是我的对象。

export const loginValidator = yup.object({
  login: yup.string().when('password', {
    is: (v) => v < 9999 && v > 999,
    then: yup.string().required('Phone No is required').matches(phoneRegExp, 'Must be 10 digits'),
    otherwise: yup.string().email().required('Email is required'),
  }),
  password: yup.string().when('login', {
    is: (m) => phoneRegExp.test(m),
    then: yup
      .string()
      .required('Pin is Required')
      .matches(/^[0-9]+$/, 'Must be only digits')
      .min(5, 'Must be exactly 5 digits')
      .max(5, 'Must be exactly 5 digits'),
    otherwise: yup.string().required('password is required'),
  }),
});

我遇到的错误

Error: Cyclic dependency, node was:"password"

这有什么问题 谢谢!!

【问题讨论】:

  • 它是循环依赖,因为你的密码依赖于登录,你的登录依赖于密码。
  • 我假设密码需要登录时登录不需要密码
  • 解决办法是什么

标签: reactjs react-native formik yup


【解决方案1】:

为了避免循环依赖,必须将这些值添加到 yup。

[['login', 'password']]

例子,

export const loginValidator = yup.object().shape(
  {
    login: yup.string().when('password', {
      is: (v) => v < 100000,
      then: yup.string().required('Phone No is required').matches(phoneRegExp, 'Must be 10 digits'),
      otherwise: yup.string().email().required('Email is required'),
    }),
    password: yup.string().when('login', {
      is: (m) => phoneRegExp.test(m),
      then: yup
        .string()
        .required('Pin is Required')
        .matches(/^[0-9]+$/, 'Must be only digits')
        .min(5, 'Must be exactly 5 digits')
        .max(5, 'Must be exactly 5 digits'),
      otherwise: yup.string().required('password is required'),
    }),
  },
  [['login', 'password']]
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    相关资源
    最近更新 更多