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.

[QUESTION] Yup validation of multi level object

See original GitHub issue

I am using Yup with formik. My values look like this:

  "values": {
    "startTime": "2019-10-13T01:04:11.596Z",
    "endTime": null,
    "phraseType": {
      "id": "HELLO"
    }

  phraseValue: Yup.number().positive().when('phraseType', {
    is: (val) => val.id === 'HELLO',
    then: Yup.string().required('required'),
    otherwise: Yup.string().notRequired()
  }),

It doesn’t seem I am able to access the id off of the val object. Val is being returned as a string “[Object][Object]” instead of the actual object. How do I use when() with a key to get back an object?

Thanks!

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
nissanecommented, Dec 11, 2019

ok thanks, I got everything up to the latest version. I found my problem. In my code I also had: phraseType: yup.string().required(), that needed to be phraseType: yup.object().required().

I updated my example if your interested. if you use the line: phraseType: yup.string().required(),

the validation fails

if you change it to: phraseType: yup.object().required(),

validation works. Not a bug, just letting you know. really appreciate you researching.

0reactions
jquensecommented, Dec 11, 2019

if you’re still stuck the reason your example is throwing is because phaseType is not defined in your schema.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup nested schema validation - Stack Overflow
I'm trying to validate an object conditionally on a Select Option that's been chosen by a user, the issue is I'm rendering a...
Read more >
Yup Email Validation: String Schema Code Examples
The Yup object schema provides two methods for validating: isValid ... Yup also gives us the ability to validate multiple emails at once, ......
Read more >
How to internationalize a Yup validation schema in ... - Medium
In the CodeSandbox below, we have a basic setup of React, react-i18next, Formik and Yup. We display a form with a required email...
Read more >
React Multi-Step Form Tutorial: Using Formik, Yup ... - YouTube
In this tutorial, we'll build a multi -step form using React, Formik and Yup for validation.In order to validate our forms, we will...
Read more >
Formik with Advanced Yup Validation - React Forms - YouTube
In this tutorial we will check out the powerful validation we can do by integrating Yup with Formik!NEW TO FORMIK? Check out my...
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