Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to require either one of two fields? (Cyclic dependency error) #79

Closed
supermoos opened this issue Jan 20, 2017 · 9 comments
Closed

Comments

@supermoos
Copy link

What (if possible) is the correct way to define that either the email or phone is a string > 1?

    var formModelSchema = yup.object({
        email: yup.string().email().when('phone', {
            is: (phone) => !phone || phone.length === 0,
            then: yup.string().email().required(),
            otherwise: yup.string()
        }),
        phone: yup.string().when('email', {
            is: (email) => !email || email.length === 0,
            then: yup.string().required(),
            otherwise: yup.string()
        })
    });

Uncaught (in promise) Error: Cyclic dependency: "phone"

@jquense
Copy link
Owner

jquense commented Jan 20, 2017

Generally I move the validation up to the object. OR don't use when and write a custom test:

yup.string().test(function (value) {
  const { email } = this.parent;
  if (!email) return value != null
  return true
})

If you want to use when there is an escape hatch you can use, as documented in this test: https://github.com/jquense/yup/blob/master/test/object.js#L609

@AndrewKvalheim
Copy link

Correction to the above link:

https://github.com/jquense/yup/blob/master@{2017-01-20}/test/object.js#L609

@dziugasmeskauskas
Copy link

dziugasmeskauskas commented May 22, 2019

Generally I move the validation up to the object. OR don't use when and write a custom test:

yup.string().test(function (value) => {
  const { email } = this.parent;
  if (!email) return value != null
  return true
})

If you want to use when there is an escape hatch you can use, as documented in this test: https://github.com/jquense/yup/blob/master/test/object.js#L609

You have a typo in the code. You are declaring a function and using an arrow afterwards. Corrected:

yup.string().test(function (value) {
  const { email } = this.parent;
  if (!email) return value != null
  return true
})

@Sletheren
Copy link

Sletheren commented Jan 23, 2020

Generally I move the validation up to the object. OR don't use when and write a custom test:

yup.string().test(function (value) => {
  const { email } = this.parent;
  if (!email) return value != null
  return true
})

If you want to use when there is an escape hatch you can use, as documented in this test: https://github.com/jquense/yup/blob/master/test/object.js#L609

If you're wondering why this is not working, then make sure you're not using an arrow function for the callback
yup.string().test((value) => {.... won't work since this is undefined :)

@GeoffreyHervet
Copy link

    var formModelSchema = yup.object({
        email: yup.string().email().when('phone', {
            is: (phone) => !phone || phone.length === 0,
            then: yup.string().email().required(),
            otherwise: yup.string()
        }),
        phone: yup.string().when('email', {
            is: (email) => !email || email.length === 0,
            then: yup.string().required(),
            otherwise: yup.string()
        })
    },
   // This is what you missed
   [ [ 'email', 'phone' ] ]
);

@Qwal
Copy link

Qwal commented Oct 7, 2020

I had to use yup.object().shape({...}) with @GeoffreyHervet solution to avoid cyclic dependency error.

@itminhnhut
Copy link

    var formModelSchema = yup.object({
        email: yup.string().email().when('phone', {
            is: (phone) => !phone || phone.length === 0,
            then: yup.string().email().required(),
            otherwise: yup.string()
        }),
        phone: yup.string().when('email', {
            is: (email) => !email || email.length === 0,
            then: yup.string().required(),
            otherwise: yup.string()
        })
    },
   // This is what you missed
   [ [ 'email', 'phone' ] ]
);

error
var validationSchemaFilter = Yup.object().shape(
{
bookingTimeBegin: Yup.string().when('bookingTimeEnd', {
is: (bookingTimeEnd) => bookingTimeEnd?.length > 0,
then: Yup.string().required('Chọn ngày bắt đầu'),
otherwise: Yup.string(),
}),
bookingTimeEnd: Yup.string().when('bookingTimeBegin', {
is: (bookingTimeBegin) => bookingTimeBegin?.length > 0,
then: Yup.string().required('Chọn ngày kết thúc'),
otherwise: Yup.string(),
}),
},
[['bookingTimeBegin', 'bookingTimeEnd']],
);

@loxosceles
Copy link

loxosceles commented Dec 15, 2021

    var formModelSchema = yup.object({
        email: yup.string().email().when('phone', {
            is: (phone) => !phone || phone.length === 0,
            then: yup.string().email().required(),
            otherwise: yup.string()
        }),
        phone: yup.string().when('email', {
            is: (email) => !email || email.length === 0,
            then: yup.string().required(),
            otherwise: yup.string()
        })
    },
   // This is what you missed
   [ [ 'email', 'phone' ] ]
);

Where is this documented? I would like to know how this works with properties of FieldArrays.

@AlenJakob
Copy link

AlenJakob commented Oct 16, 2023

Generally I move the validation up to the object. OR don't use when and write a custom test:

yup.string().test(function (value) {
  const { email } = this.parent;
  if (!email) return value != null
  return true
})

If you want to use when there is an escape hatch you can use, as documented in this test: https://github.com/jquense/yup/blob/master/test/object.ts

The file format has beend changed to .ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants