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.

How to conditionally validate at least one of n values are set?

See original GitHub issue

I have 5 form fields, at last one of which must be populated for validation to succeed. I attempted this, but soon ran into cyclic reference issues:

tag_business: yup.string().when(['tag_cost_centre', 'tag_project_code', 'tag_internal_order', 'tag_business'], {
  is: (tag_cost_centre, tag_project_code, tag_internal_order, tag_business) => {
    return !(tag_cost_centre, tag_project_code, tag_internal_order, tag_business)
  },
  then: yup.string().required()
})

What would be the best way to implement this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:28 (3 by maintainers)

github_iconTop GitHub Comments

149reactions
jquensecommented, Mar 2, 2018
var schema = yup.object().shape({
  a: yup.string().when(['b', 'c'], {
    is: (b, c) => !b && !c,
    then: yup.string().required()
  }),
  b: yup.string().when(['a', 'c'], {
    is: (a, c) => !a && !c,
    then: yup.string().required()
  }),
  c: yup.string().when(['a', 'b'], {
    is: (a, b) => !a && !b,
    then: yup.string().required()
  })
}, [['a', 'b'], ['a', 'c'], ['b','c']]) // <-- HERE!!!!!!!!
16reactions
rmrotekcommented, Sep 25, 2019

instead of using examples above, i just created a ‘ghost’ field in yup validation schema that checks specified fields, alot less code and no cyclic errors

yup.object().shape({
        a: yup.string(),
        b: yup.string(),
        AorB: yup.bool().when(['a', 'b'], {
            is: (a, b) => (!a && !b) || (!!a  && !!b),
            then: yup.bool().required('some error msg'),
            otherwise: yup.bool()
        })
    })

This will generate an error and prevent submit.

When using Formik Fields just remember to include additional errors from this ‘ghost’ field, like:

<Field
        error={!!(errors && errors.a || (errors as any).AorB)}
        helperText={errors && errors.b || (errors as any).AorB)}
/>

(errors as any) because TypeScript will not see this error (field is not in initialValues)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup - How to conditionally validate at least one of n values are ...
You can this: a: yup.string().when(['b', 'c', 'd'], { is: (...fields) => fields.some((field) => field === true), then: yup.string().required() }) ...
Read more >
yup - How to conditionally validate at least one of n ... - RunKit
1. var yup = require('yup') ; 2. var invariant = require('invariant') ; 3. var without = require("lodash.without") ; 4. ​ ; 5. yup.addMethod(yup.object,...
Read more >
How to conditionally validate at least one of n values are set ...
All you need to do is list all the pair-wise (2-tuples) combinations. As you have 4 fields, you expect to have 6 pairs...
Read more >
How to Create an Optional Dynamic Validation Schema based ...
In this lesson we'll show how to setup a nested validation structure using the `yup` library. We'll then use the `yup.lazy` method to...
Read more >
Yup conditional validation - CodeSandbox
This is an example how to build form validations for fields that only exist based on the value of another field. validation. formik....
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