cyclic dependency - all or nothing validation
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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/1006As 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?
@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
noSortEdgesseems fairly unreliable at scale. The difference between my two test cases:Is just one field:
I can’t figure out why removing that field would break it.