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.

Validate child against parent.

See original GitHub issue

Hi,

I’m having a hard time figuring this out, not sure if it’s even supported but I feel like it should be.

Basically I’m trying to mark a field as required if a referenced field is truthy. Something along the lines of

{
  requiredField: boolean(),
  nested: object().shape({
    conditional: when('$parent.requiredField', ....)
  })
}

Something similar was being discussed over at https://github.com/jaredpalmer/formik/issues/539 but that convo didn’t get very far.

Is there no way to reference a key in a parent schema?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:27
  • Comments:22 (1 by maintainers)

github_iconTop GitHub Comments

126reactions
jaclynjcommented, Jul 11, 2018

We had a similar issue, but solved it this way:

{
  requiredField: boolean(),
  nested: object().when('requiredField', {
    is: true,
    then: object({
      foo: string.required(),
      // ...etc
    })
}
13reactions
third774commented, Jun 27, 2018

I ended up passing in the entire object that I’m trying to validate as context so I could grab the values I care about.

const mySchema = {
  requiredField: boolean(),
  nested: object().shape({
    conditional: string().when("$requiredField", (requiredField, schema) =>
      requiredField ? schema.required() : schema
    )
  })
}

const myObj = {
  requiredField: true,
  nested: {
    conditional: "Some string"
  }
}

const isValid = mySchema.isValidSync(myObj, {context: myObj})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why It Is So Important For Parents to Validate Their Children
Validation is a way of letting someone know we understand him or her. Being understood is an essential ingredient to feeling connected and...
Read more >
Emotional Validation: How to Validate a Child's Feelings (33 ...
Validation can be used with a child who is showing negative emotions. It's also one of the most effective ways to calm down...
Read more >
Validating Your Child's Feelings: the How's and Why's
Emotional validation teaches your kids that feeling and expressing their emotions is OK. Parents who validate their kids' emotions model that it's natural...
Read more >
5 Simple Statements to Validate Your Child's Feelings
Parental validation doesn't condone poor behavior; rather, it helps a child to feel seen, heard, and valued. · It also serves to deescalate ......
Read more >
Parenting With Validation - Kurtz Psychology
Validation encourages children to share their feelings and encourages open communication about emotions. Through validation, a parent can teach ...
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