cyclic dependency - all or nothing validation

See original GitHub issue

Describe the bug cyclic dependency when performing all or nothing validation

To Reproduce

I’m trying to perform all or nothing validation. In other words, if either field1 or field2 exist, then both should be required(). However, if neither exists, they’re both optional.

I’m using the below code:

import * as yup from 'yup';

export default class Main {
  static async run() {
    const schema = yup.object().shape({
      field1: yup.string().when(
        'field2',
        {
          is: (value) => !!value,
          then: yup.string().required(),
        }
      ),
      field2: yup.string().when(
        'field1',
        {
          is: (value) => !!value,
          then: yup.string().required(),
        }
      ),
    });
    try {
      await schema.validate({
        field1: 'foo',
      });
    } catch (e) {
      console.log('e=',e);
    }
  }
}

Main.run();

Downloadable version available here: https://github.com/createthis/yup_all_or_nothing_validation

When I run this, I get the error: UnhandledPromiseRejectionWarning: Error: Cyclic dependency, node was:"field2"

Expected behavior I thought I would receive a yup validation error, not a cyclic dependency error.

Platform (please complete the following information):

  • node 14.18.3
  • yup 0.32.11

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
createthiscommented, Jan 21, 2022

@jquense Or maybe you are referring to this one, where the user adds an array of string field names as a second argument to shape()? https://github.com/jquense/yup/issues/1006

As far as I can tell, the documentation does not seem to mention what the second argument array does: https://github.com/jquense/yup#objectshapefields-object-nosortedges-arraystring-string-schema

Perhaps you can re-open this issue until the second argument array has been documented?

0reactions
createthiscommented, Mar 7, 2022

@jquense I had some more time to play with this today, so I created a repository with some examples: https://github.com/createthis/yup_all_or_nothing

Maybe it is something I am doing, but noSortEdges seems fairly unreliable at scale. The difference between my two test cases:

Is just one field:

--- test_2.js	2022-03-07 15:53:51.000000000 -0500
+++ test_fail.js	2022-03-07 16:04:04.000000000 -0500
@@ -13,7 +13,6 @@
       company: {
         name_legal: 'Name Legal',
         first_name: 'First Name',
-        age: 'Age',
       },
       owner1: {
         first_name1: 'First Name1',

I can’t figure out why removing that field would break it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cyclic dependency issue #193 - jquense/yup - GitHub
Hi, I am having a issue with cyclic dependency within a schema. ... correct approach, when you have scenario with 'all or nothing'...
Read more >
Error: Cyclic dependency with Yup Validation - Stack Overflow
Save this question. Show activity on this post. I have this Yup validation schema that includes a check on a min_amount and max_amount...
Read more >
yup: Conditional Validation & Cyclic Dependency Error Fix
While using yup, there might be a scenario where you might have to make one field required if another field has a certain...
Read more >
PH29775: CIRCULAR DEPENDENCY VALIDATION TAKES ...
Description: When you?re writing field dependencies or dependent picklists, the validation of circular dependencies does not ignore the ...
Read more >
Cut the validation <-> txmempool circular dependency
A circular dependency exists when two or more modules depend on each other. · One of the circular dependencies in the codebase is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found