Yup 字段等于时的条件验证

问题描述 投票:0回答:0

我需要这样的架构:

const schema = yup.object().shape({
SKU: yup.string().required(),
name: yup.string().required(),
price: yup.number().required(),
type: yup.string().required(),
size: yup
  .number()
  .moreThan(0)
  .when('type', {
    is: (type) => type === 'DVD',
    then: yup.number().moreThan(0).required(),
  }),
height: yup
  .number()
  .moreThan(0)
  .when('type', {
    is: (type) => type === 'furniture',
    then: yup.number().moreThan(0).required(),
  }),
width: yup
  .number()
  .moreThan(0)
  .when('type', {
    is: (type) => type === 'furniture',
    then: yup.number().moreThan(0).required(),
  }),
length: yup
  .number()
  .moreThan(0)
  .when('type', {
    is: (type) => type === 'furniture',
    then: yup.number().moreThan(0).required(),
  }),
weight: yup
  .number()
  .moreThan(0)
  .when('type', {
    is: (type) => type === 'book',
    then: yup.number().moreThan(0).required(),
  }),
});

当字段类型等于“DVD”时,我想将大小字段设置为必填项等。 但是零件

is: (type) => type === 'DVD'
等发出警告:

警告:TypeError: branch is not a function 在验证期间捕获了未处理的错误

如何解决这个问题?

javascript reactjs forms formik yup
© www.soinside.com 2019 - 2024. All rights reserved.