0

In my application I'm using Yup validation. I faced a scenario where I need at least one of the three fields(String) required. I've tried using the below code but it is throwing Uncaught Error: Cyclic dependency, the node was: "b".

a: yup.string().when(['b', 'c'], {
 is: (b, c) => !b && !c,
 then: yup.string().required()
}),
b: yup.string().when(['a', 'c'], {
 is: (a, c) => !a && !c,
 then: yup.string().required()
}),
c: yup.string().when(['a', 'b'], {
 is: (a, b) => !a && !b,
 then: yup.string().required()
})
}, [['a', 'b'], ['a', 'c'], ['b','c']])```

Any response or working code would be very helpful. Thanks in advance.
dinesh dsv
  • 13
  • 1
  • 4

1 Answers1

3

I found that you can do this using the lazy construct in Yup.

Ref for lazy: https://github.com/jquense/yup#yuplazyvalue-any--schema-lazy

Creates a schema that is evaluated at validation/cast time. Useful for creating recursive schema like Trees, for polymorphic fields and arrays.

Example:

a: yup.lazy(() => yup.string().when(['b', 'c'], {
 is: (b, c) => !b && !c,
 then: yup.string().required()
})),
b: yup.lazy(() => yup.string().when(['a', 'c'], {
 is: (a, c) => !a && !c,
 then: yup.string().required()
})),
c: yup.lazy(() => yup.string().when(['a', 'b'], {
 is: (a, b) => !a && !b,
 then: yup.string().required()
}))
}, [['a', 'b'], ['a', 'c'], ['b','c']])```
Cosmo
  • 296
  • 3
  • 2