Yup 中的条件验证

Pav*_*van 1 javascript forms reactjs yup formik

如何实现像 1 个字段的值应该总是大于另一个字段的值这样的条件。

这是我的架构

   value: Yup.number().required(''),
   value2: Yup.number().when('value',{
     is: (val) => something here
    then: Yup.number().required('Required').positive('value should be positive'),
    otherwise: Yup.number()
  })
Run Code Online (Sandbox Code Playgroud)

我想检查 value2 是否总是 > value。如何在 when 条件下访问 value2 的值?

cap*_*013 5

我不确定这是正确的方法,但我正在使用test

像这样:

yourField: Yup.string().test('should-be-greather-than-yourField2', 'Should be greather than yourfield 2', function(value) {
  const otherFieldValue = this.parent.yourField2

  if (!otherFieldValue) {
    return true;
  }
  return value > otherFieldValue;
})
Run Code Online (Sandbox Code Playgroud)