繁体   English   中英

是的,循环依赖:两个相互要求的字段

[英]Yup cyclic dependency: Two fields mutually requiring each other

我有以下是的验证模式

 const validationSchema = Yup.object().shape({
  name: Yup.string(),
  services: Yup.array(Yup.string().oneOf(SERVICES, "Invalid service!")),
  locations: Yup.array(Yup.string().oneOf(LOCATIONS, "Invalid location!")),
  distance: Yup.number()
    .typeError("Invalid distance!")
    .positive("Invalid distance!")
    .when("userFormattedAddress", {
      is: (val) => !!val,
      then: Yup.number().required(),
      otherwise: Yup.number(),
    }),
  userFormattedAddress: Yup.string("Invalid user location!").when("distance", {
    is: (val) => !!val,
    then: Yup.string().required(),
    otherwise: Yup.string(),
  }),
  userCoordinates: Yup.array(
    Yup.number("Invalid user location!").positive("Invalid user location!")
  ),
});

期望的行为是当输入距离时,用户必须输入地址,并且当用户输入地址时,他们也必须指定距离。 但是,我遇到了循环依赖……有什么想法吗? 谢谢!

好的,我找到了答案:

 const validationSchema = Yup.object().shape({
 name: Yup.string(),
 services: Yup.array(Yup.string().oneOf(SERVICES, "Invalid service!")),
 locations: Yup.array(Yup.string().oneOf(LOCATIONS, "Invalid location!")),
 distance: Yup.number()
   .typeError("Invalid distance!")
   .positive("Invalid distance!")
   .when("userFormattedAddress", {
     is: (val) => !!val,
     then: Yup.number().required(),
     otherwise: Yup.number(),
   }),
 userFormattedAddress: Yup.string("Invalid user location!").when("distance", {
   is: (val) => !!val,
   then: Yup.string().required(),
   otherwise: Yup.string(),
 }),
 userCoordinates: Yup.array(
   Yup.number("Invalid user location!").positive("Invalid user location!")
 ),
}, ["distance", "userFormattedAddress"]);

您需要将数组中的字段作为 noSortedEdges 参数传递的位置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM