question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cyclic dependency issue

See original GitHub issue

Hi, I am having a issue with cyclic dependency within a schema. I have looked at this issue, but what was suggested didn’t work for me.

yup.object().shape(
  {
    sub_building: yup.string(),
    building_name: yup.string().when('building_number', {
      is: building_number => !building_number,
      then: yup.string().required(),
    }),
    building_number: yup.string().when('building_name', {
      is: building_name => !building_name,
      then: yup.string().required(),
    }),
    street: yup.string().required(),
    town: yup.string().required(),
    county: yup.string().required(),
  },
  ['sub_building', 'building_name', 'building_number', 'street', 'town', 'county']
);

This is my schema. Basically i would like either building_number or building_name to be filled.

Thanks for your help in advance.

Issue Analytics

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

github_iconTop GitHub Comments

49reactions
davidchasecommented, Apr 23, 2018

Both ideas from https://github.com/jquense/yup/issues/79#issuecomment-274174656 worked for me

const inst = yup.object({
  location: yup
    .object({
      state: yup.string(),
      county: yup.string()
    })
    .test(
      'is-optional',
      '${path}.state or ${path}.county is required',
      function({ state, county }) {
        return state === '' && county === '' ? false : true
      }
    )
})
// throws ValidationError: location.state or location.county is required
inst.validate({
    location: {
     state: '',
     county: ''
    }
})
// doesn't throw
inst.validate({
    location: {
     state: 'CA',
     county: ''
    }
})

or

const inst2 = yup.object().shape(
  {
    location: yup.object().shape({
      state: yup
        .string()
        .when('county', {
           is: '',
           then: yup.string().required(),
           otherwise: yup.string()
        }),
      county: yup
        .string()
        .when('state', {
           is: '',
           then: yup.string().required(),
           otherwise: yup.string()
        })
    }, ['county', 'state'])
  }
)

I needed either county or state to be filled in

20reactions
jquensecommented, Nov 12, 2018

@cenda the second argument to object.shape() is not an array of conflicting fields. its an array of field pairs. e.g. [['street', 'zip_code'], ['zip_code', 'city']] you need to make the list exhaustive

Read more comments on GitHub >

github_iconTop Results From Across the Web

Circular dependency - Wikipedia
In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other...
Read more >
What is a circular dependency and how can I solve it?
A circular dependency is where Project A depends on something in Project B and project B depends on something in Project A. This...
Read more >
Cyclic Dependency - an overview | ScienceDirect Topics
A cyclic dependency is formed when two or more abstractions have direct or indirect dependencies on each other. Cyclic dependencies between abstractions ...
Read more >
Circular Dependencies in Spring - Baeldung
When we have a circular dependency, it's likely we have a design problem and that the responsibilities are not well separated.
Read more >
How to fix nasty circular dependency issues once and for all in ...
The module loading order in JavaScript is deterministic. Yet it is not very easy to follow in large projects. The reason for this,...
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