是的循环依赖:两个领域相互依赖

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 参数传递的地方