Reactjs Yup验证两个字段之一是必需的(其中一个是数字数组)

Reactjs Yup验证两个字段之一是必需的(其中一个是数字数组),reactjs,typescript,validation,formik,yup,Reactjs,Typescript,Validation,Formik,Yup,我将Formik与Yup和Typescript一起使用,并将其作为表单的初始值 const initialValues = { title: "", overview: "", related_items_id: [], short_desc: "" }; 这是我的模式 const formSchema = Yup.object().shape({ title: Yup.string() .trim() .required

我将Formik与Yup和Typescript一起使用,并将其作为表单的初始值

const initialValues = {
    title: "",
    overview: "",
    related_items_id: [],
    short_desc: ""
};
这是我的模式

const formSchema = Yup.object().shape({
    title: Yup.string()
        .trim()
        .required("This field is required."),
    overview: Yup.string().required("This field is required."),
    related_items_id: Yup.array()
        .min(1, "Pick at least 1 item")
        .of(Yup.number().required("This field is required.")),
    short_desc: Yup.string().required("This field is required.")
});
现在,我需要
related\u items\u id
数组或
short\u desc
数组,如果其中一个填充了数据,另一个则不是必需的,反之亦然,我如何在yup中使用类似
when
的东西来实现这一点

下面是我创建的一个codesandbox,它显示了我在尝试使用Yup的
when
方法时遇到的错误


您可以通过创建一个相互依赖于
相关项目\u id
short\u desc

export interface BaseType {
    title: string;
    overview: string;
}

interface RelatedItemsType extends BaseType {
  related_items_id?: Array<any>;
  short_desc?: never;
}

interface ShortDescType extends BaseType {
  related_items_id?: never;
  short_desc?: string;
}

export type InitialValueType = RelatedItemsType | ShortDescType;
要有条件地设置formSchema,请签出文档


这是您检查其中是否至少有一个已完成的方式

const schema = yup.object().shape({
  'fieldOneName': Yup.string()
  .when('fieldTwoName', {
    is: (fieldTwo) => !fieldTwo || fieldTwo.length === 0,
    then: Yup.string()
      .required('At least one of the fields is required'),
  }),
  'fieldTwoName': Yup.string()
  .when(codiceFiscale.name, {
    is: (fieldOne) => !fieldOne || fieldOne.length === 0,
    then: Yup.string().
      .required('At least one of the fields is required'),,
  })
}, ['fieldOneName', 'fieldTwoName']) // <-- HERE!!!!!!!!
const schema=yup.object().shape({
'fieldOneName':是的。字符串()
.when('fieldTwoName'{
is:(fieldTwo)=>!fieldTwo | | fieldTwo.length==0,
然后:是的,string()
.required(“至少需要一个字段”),
}),
“fieldTwoName”:是的。字符串()
.when(codiceFiscale.name{
is:(fieldOne)=>!fieldOne | | fieldOne.length==0,
然后:是的,字符串()。
.required('至少需要一个字段'),,
})
},['fieldOneName','fieldTwoName'])/
!mobile | | mobile.length==0,
然后:是的,string()
.required(“至少需要一个字段”),
}),                                                                  
“mobile”:是的。字符串()
.when('电子邮件',{
is:(email)=>!email | | email.length==0,
然后:是的,string()
.required('至少需要一个字段')
})                                                                   
},[“电子邮件”,“手机])
}                                                                                                        

感谢您的努力,但您能否提供一个模式的示例,因为这是让我困惑的部分,而不是Typescript部分。我用一个示例编辑了我的答案,说明了如何操作@RubyThanks我的朋友,在问这个问题之前我实际上已经走了这么远,只是我总是会犯这个错误。。。循环依赖,节点是:“short_desc”。我不知道为什么。当我尝试用谷歌搜索它时,我发现有人这样做,但这对我没有帮助。。。我认为您应该将第三个参数传递给
Yup.object().shape
,它是一个数组,但我不知道该数组的形状是什么。我将尝试使用一个代码沙盒来演示这一点。当然,创建一个沙盒并共享它。
const basicFormSchema = Yup.object().shape(
    {
      title: Yup.string()
        .trim()
        .required("This field is required."),
      overview: Yup.string().required("This field is required."),
      related_items_id: Yup.array().when("short_desc", {
        is: "",
        then: Yup.array()
          .min(1, "Pick at least 1 item")
          .of(Yup.number().required("This field is required.")),
        otherwise: Yup.array()
      }),
      short_desc: Yup.string().when("related_items_id", {
        is: relatedItemsId => relatedItemsId.length === 0,
        then: Yup.string().required("This field is required."),
        otherwise: Yup.string()
      })
    },
    [["related_items_id", "short_desc"]]
  );
const schema = yup.object().shape({
  'fieldOneName': Yup.string()
  .when('fieldTwoName', {
    is: (fieldTwo) => !fieldTwo || fieldTwo.length === 0,
    then: Yup.string()
      .required('At least one of the fields is required'),
  }),
  'fieldTwoName': Yup.string()
  .when(codiceFiscale.name, {
    is: (fieldOne) => !fieldOne || fieldOne.length === 0,
    then: Yup.string().
      .required('At least one of the fields is required'),,
  })
}, ['fieldOneName', 'fieldTwoName']) // <-- HERE!!!!!!!!
<Formik
            initialValues={{
                email: '',
                mobile: '',
                submit: null,
            }}

            validationSchema = {                                                                                         
    Yup.object().shape({                                                                                 
            'email': Yup.string()                                                                        
                        .when('mobile', {                                                    
                        is: (mobile) => !mobile || mobile.length === 0,                      
                        then: Yup.string()                                                   
                        .required('At least one of the fields is required'),                 
                                    }),                                                                  
            'mobile': Yup.string()                                                                       
                        .when('email', {                                                     
                        is: (email) => !email || email.length === 0,                         
                        then: Yup.string()
                        .required('At least one of the fields is required')
                        })                                                                   
    }, ['email', 'mobile'])                                                                              
}