How to access to parent of parent using test method?

See original GitHub issue

I know at the moment it’s not possible to access to parent.parent inside test method (+), just wondering is there another approach to have such functionality, let’s say I want the price field be required if type is equal to 1.

tickets: yup
  .array()
  .of(
    yup.object().shape({
      title: yup.string().required(),
      type: yup.number().required(),
      availability: yup
        .object()
        .shape({
          from: yup
            .date()
            .when(
              "to",
              (expiryDate: any, schema: yup.DateSchema) =>
                expiryDate &&
                schema.max(
                  expiryDate,
                  "Available From must be before Avalable To"
                )
            ),
          to: yup.date()
        })
        .nullable(true),
      priceInfo: yup
        .object()
        .shape({
          price: yup
            .mixed()
            .test("match", "price is required", function(price) {
              console.log(this);
              return false;
            }),
          currency: yup.string(),
          fee: yup.number(),
          discount: yup.object().shape({
            type: yup.number(),
            value: yup.number()
          })
        })
        .nullable(true)
    })
  )

Issue Analytics

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

github_iconTop GitHub Comments

16reactions
jamitercommented, Sep 10, 2021

There is a way to find the parent of the parent. Use context.from. It’s an array of parents:

const [parent1, parent2, ...etc] = context.from;

This isn’t correctly typed yet though: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/49512

I found this mentioned here: https://github.com/jquense/yup/issues/735#issuecomment-873828710, but doesn’t seem to be documented for some reason.

4reactions
geocinecommented, Feb 10, 2020

Just in case someone ends up here 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

This only works on the parent just 1 level up. How do you access the sibling of that parent?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yup validation access parent.parent - Stack Overflow
You can use the test method available in the https://github.com/jquense/yup#mixedtestname-string ...
Read more >
.parent() | Selector Object | Test API | API | Docs - TestCafe
Selector.parent Method ... Returns an array of parent elements, starting with the closest relatives. Syntax. parent. Selector().parent() → Selector.
Read more >
Parent Property | TestComplete Documentation
Use an object's Parent property to get its parent by reference. If the object does not have a parent, the property returns an...
Read more >
parent | Cypress Documentation
Get the parent DOM element of a set of DOM elements. Please note that .parent() only travels a single level up the DOM...
Read more >
Parent Object - ADM Help Centers
Parent.<supported method>. Note: Using the Context object here enables you to access the run-time context of the selected activity and parent activity.
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