Is there any way to get a parent value in `when` and `test` ?

See original GitHub issue
var inst = yup.object({
  isBig: yup.boolean(),
  id: yup.number(),
  counter: yup.object({
     // get isBig from parent.parent in when
     time: yup.number().when('isBig', (isBig, schema) => {
       return isBig ? schema.min(1) : schema.min(1897778);
    }),
    value: yup.number().when('isBig', (isBig, schema) => {
       return isBig ? schema.min(5) : schema.min(0);
    }),
     // get id from parent.parent in test
    productId: yup().number().test('productId', 'Product option with id ${value} not found', async function (id) {
        if(this.parent.parent.id) {
            return true
        }

        ....
    })
  })
});

inst.validate({ isBig: false, count: 4 });

Can I get in nested objects get parent value in when in counter.time get isBig and in test get counter.productId get id ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
MikeSuitercommented, Mar 13, 2019

@khotey Did you ever figure a way to do this or @jquense did this ever get implemented?

6reactions
oleztcommented, Feb 7, 2020

From the documentation: test functions are called with a special context, or this value, that exposes some useful metadata and functions. Note that to use the this context the test function must be a function expression (function test(value) {}), not an arrow function, since arrow functions have lexical context.

this.parent: in the case of nested schema, this is the value of the parent object

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup validation access parent.parent - Stack Overflow
I used this workthrough with success. Idea: Pass whole form-data as a context to the schema and access any form value using. this.options....
Read more >
How do you populate a parent value in a test class
My question is how do you pass parent field values to helper classes in test without creating the whole record and then querying...
Read more >
Parent Property | TestComplete Documentation
A test item can have a number of child test items. The TestItem.Parent property returns the parent test item of the TestItemObj ....
Read more >
The fork() System Call
This can be done by testing the returned value of fork(): ... Please note that Unix will make an exact copy of the...
Read more >
.parent() | jQuery API Documentation
Description: Get the parent of each element in the current set of matched elements ... If the selector is supplied, the elements will...
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