如何访问 yup 对象中的输入值?

How can I access the value of input inside yup object?

我在 react 中使用 formik 进行验证,但现在我需要 yup.object({}) 中 firstName 的值。我怎样才能访问它?

const validationSchema = yup.object({
        firstName: yup
          .string("Enter your First Name")
          .required("First Name is required"),
    })
 const formik = useFormik({
    initialValues: {
      firstName: ""
    },
    validationSchema: validationSchema,
})

你可以使用 test 来完成这个

const validationSchema = yup.object({
        firstName: yup
          .string("Enter your First Name")
          .test(name for the test, validation message, (value) => this gives your value)
    })

参考

1.Using Test in Yup

2.Using When in Yup