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.

Conditionally required schema field

See original GitHub issue

Is it possible? Smth like this:

const schema = yup.object({
  username: yup.string().conditionallyRequired(someVar === anotherVar)
})

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:21
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

62reactions
ronaldozanonicommented, Nov 24, 2019

@nilamsavani91 You want something like this:

const schema = Yup.object().shape({
  dateField: Yup.date()
    .required('Date is required')
    .typeError('Invalid date'),
  timeField: Yup.string()
    .when('dateField', {
      is: (dateField) => dateField.length > 0,
      then: Yup.string()
        .required('Time field is required')
        .typeError('Time field is required')
    })
});

See more here https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema

51reactions
yantakuscommented, May 29, 2017

Thank you. I’ve already achieved it with the following code:

const schema = yup.object({
  password: yup
    .string()
    .when('$user', (user, passSchema) => user
      ? passSchema
      : passSchema.min(6, 'minimal password length is 6 characters').required()),
});
...
<Form
  context={{ user }}
>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Applying Subschemas Conditionally - JSON Schema
The dependentSchemas keyword conditionally applies a subschema when a given property is present. This schema is applied in the same way allOf applies...
Read more >
jsonSchema attribute conditionally required - Stack Overflow
I can think of four different ways to conditionally require a field. Dependencies. The dependentSchemas keyword is a conditional way to apply a ......
Read more >
How to implement conditional required field validation
Hi, I am trying to implement condition based required validation. But its not working. I have dropdown with values circle, square and rectangle....
Read more >
Applying subschemas conditionally | Opis JSON Schema
This is a conditional structure containing three keywords: if , then and else . Every keyword value must be a valid JSON schema...
Read more >
Field required based on condition · Issue #539 · json-schema ...
It is very common to have data structures in which a field becomes required only if a particular condition holds.
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