Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent selector (TestContext.from) skips array schemas. #2187

Open
Sam-Apostel opened this issue Mar 21, 2024 · 2 comments
Open

Parent selector (TestContext.from) skips array schemas. #2187

Sam-Apostel opened this issue Mar 21, 2024 · 2 comments

Comments

@Sam-Apostel
Copy link

The TestContext.from property is not documented yet. It was introduced in #556 without any documentation or typescript support. In an attempt to document this I stumbled upon an irregularity.

-> from seems to skip parents that are arrays.

This behavior looks like it is not accidental though since it is used in the tests:

yup/test/mixed.ts

Lines 487 to 524 in 5c77e0d

it('tests should be able to access nested parent', async () => {
let finalFrom: any, finalOptions: any;
let testFixture = {
firstField: 'test',
second: [
{
thirdField: 'test3',
},
{
thirdField: 'test4',
},
],
};
let third = object({
thirdField: mixed().test({
test() {
finalFrom = this.from!;
finalOptions = this.options;
return true;
},
}),
});
let second = array().of(third);
let first = object({
firstField: mixed(),
second,
});
await first.validate(testFixture);
expect(finalFrom[0].value).toEqual(testFixture.second[finalOptions.index]);
expect(finalFrom[0].schema).toBe(third);
expect(finalFrom[1].value).toBe(testFixture);
expect(finalFrom[1].schema).toBe(first);
});

Actual

expect(finalFrom[0].value).toEqual(testFixture.second[finalOptions.index]);
expect(finalFrom[0].schema).toBe(third);
expect(finalFrom[1].value).toBe(testFixture);
expect(finalFrom[1].schema).toBe(first);

Expected

Reading the information from the PR I expected following test cases to be valid.

expect(finalFrom[0].value).toEqual(testFixture.second[finalOptions.index]);
expect(finalFrom[0].schema).toBe(third);
expect(finalFrom[1].value).toBe(testFixture.second);
expect(finalFrom[1].schema).toBe(second);
expect(finalFrom[2].value).toBe(testFixture);
expect(finalFrom[2].schema).toBe(first);

How could we describe current behavior?
Is the current behavior what we expect the from property to behave like?

@Sam-Apostel
Copy link
Author

@akmjenkins you might have some insights on this?

@akmjenkins
Copy link
Contributor

Sorry @Sam-Apostel, I just noticed the ping.

yup underwent a reasonably significant refactoring to add typescript support after this functionality was originally introduced, I'm not sure the original PR for this functionality would be accepted if it was done again today (that's a question for @jquense)

In any case, it does indeed look like an oversight. I can't exactly remember why it might not have been built to your expectations (which I agree with, those assertions do make sense).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants