Skip to content
Advertisement

Yup validation: cyclic dependency error with multiple dependent fields

I’m using Yup to validate 3 fields which are all dependent on each other. fieldA, fieldB and fieldC. They are numbers and at least one of them needs to have a value > 0.

I’m trying to solve it like this:

JavaScript

This worked fine with only 2 fields fieldA and fieldB, where each one only had the other field passed in when(... but since introducing a third field, I now have a cyclic dependency. Do I need a completely different approach, ie. an external validation function or am I missing some detail here?

Advertisement

Answer

Your dependencies array is wrong, needs to be [[string, string]], hence you cannot bind all 3 of your fields inside. You have to do it in combinations as

[['fieldA', 'fieldB'], ['fieldA', 'fieldC'], ['fieldB','fieldC']]

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement