-
Notifications
You must be signed in to change notification settings - Fork 932
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
Allow access to parent schema (and unlimited ancestors!) in test context #556
Conversation
…llows access to unlimited ancestors
I've updated this PR, hope you don't mind. I know accessing multiple ancestors has been an issue people have been trying to solve around here and I thought that this PR could be easily expanded to solve it. Ideally, the existing
This should make work regarding #201 a breeze. |
Thought I'd add a newer comment to override my older one. The most up to date proposal is for accessing parent(s) and parent schema(s) in a test context is to look like this:
|
@jquense we need this desperately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry the delay folks, short on time!
src/object.js
Outdated
@@ -143,6 +145,9 @@ inherits(ObjectSchema, MixedSchema, { | |||
return value; | |||
} | |||
|
|||
from = originalValue | |||
? from | |||
: [...from].splice(0, 1, { schema: this, value: originalValue }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splice
returns the removed item not the array, is that what you want here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's not what I wanted to do, thanks for the catch. I've updated this.
what happens if you have an array inbetween? |
…ema context to the from array
…ntext in the array.
I've |
@akmjenkins any update on this? it takes me 3+ misrable days googleing/check issues/the Yup source code and realize it is not possible to get values from ancestors... |
This could really use documentation and this is missing from |
i need documenation for this |
For anyone wondering how this could be used this is how. const schema = Yup.object().shape({
origin: Yup.object().shape({
location: Yup.string().required(),
port: Yup.string()
.nullable()
.test('customValidation', 'Error', function(val) {
const { parent, from } = this;
// Before we can only access the immediate parent values
// of this property which is `origin` so we can get `parent.location`
// But what if we want to get `destintation.location` , with parent, this is not possible
// now we can, using, `from[1].value.destination.location` for getting the value
})
}),
destination: Yup.object().shape({
location: Yup.string().required(),
port: Yup.string().required()
})
}); From the context of So getting back at the example above Aside from values, this also introduced a way of getting the |
If you wanted to get Also, I was hoping this would make it relatively trivial to implement relative references, but using toposort to sort the graph has made things a little more complicated for me in this regard, hoping to get to it soon. |
Oh yeah right I will correct that. Thanks for this @akmjenkins |
I have just come across this issue. I upgraded Yup in my project from version 0.28.1 to 0.29.1 |
Are the typescript Types updated? I do not see anything with |
awesome work guys, thank you! |
will it work for when too? |
This still isn't in the docs. |
You can try stuffing the values object into the context parameter during validation, then accessing the values with the "$" context prefix. This worked for me, but it seems to have some limitations of its own. |
Why is this not documented? It's very useful! |
I made a terrible attempt at this PR earlier. This should do it.
This resolves #551 and adds the capability to help with #553.