Yup: Supporting this.parent.parent call

Created on 15 Jan 2018  路  8Comments  路  Source: jquense/yup

Hi there,
Could you let me know how can one traverse the schema in the test method and reach a grandparent (this.parent.parent). this.parent works fine but this.parent.parent === undefined.

Thanks,

Most helpful comment

The only workaround would be to stick your top level value in context when you validate and reference that in the test

All 8 comments

there isn't a way to do this at the moment. only the current parent is stored

Is there a way to validate array of objects in an object?
{min: 10, values: [{moreThanMin: 11}]}

I know that this is being worked on the #201 (a WIP), but do we have a workaround for it? I need to display a field depending the value of another field and I couldn't find a way to access the info because I cannot access a sibling node from its parent node :(

The only workaround would be to stick your top level value in context when you validate and reference that in the test

thanks @jquense

// my data structure
{
   ipVersion: 4,    
   tunnel: {
       gateway: {
            ip: 'xxxx',   <--- IPv4 or 6 depend on top level  `ipVersion`
            mac: 'xxx'
       }
   }
}


.when('ipVersion', {  
   is: (value,  originalValue) => {   <-- why not pass the full 'values' so we can get the value I want?
        return originalValue.ipVersion === 4;
    },
    //  getValue: originalValue => originalValue.ipVersion <-- or custom getter ?
     then: ipv4(),
     otherwise: ipv6()
 })

since I am seeing the originalValue this there in the conditional.js already:

image

And, this use case seems to be a very universal requirement IMO,
(I have seeing several similar issue like https://github.com/jaredpalmer/formik/pull/522, https://github.com/jaredpalmer/formik/issues/506, https://github.com/jaredpalmer/formik/issues/503), why can't we have this in Yup so we don't have to make some hacky workaround?

@yanzou The only thing keeping anyone from having it is someone writing it. If you want to see it in yup send a PR

@jquense how about those 2 PRs?
https://github.com/jquense/yup/pull/201
https://github.com/jquense/yup/pull/556
I am seeing people trying to fix the issue but no more grogress

Was this page helpful?
0 / 5 - 0 ratings

Related issues

the-daniel-rothig picture the-daniel-rothig  路  4Comments

AmineIT picture AmineIT  路  3Comments

you-fail-me picture you-fail-me  路  4Comments

cfteric picture cfteric  路  3Comments

RobBednark picture RobBednark  路  3Comments