-
Notifications
You must be signed in to change notification settings - Fork 907
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
feat: add schema.asContext and array.indexContextKey #1872
base: master
Are you sure you want to change the base?
Conversation
As instructed by the TODO comment
Hello @jquense, hope all is good. Please let me know what you think about this approach. Cheers |
Hey @danielpza this is cool! I think API wise though it feels like it's more complicated than it needs to be? Ultimately this API is solving the problem of not being able to reference ancestors with plain paths right? Maybe we should just solve that problem instead of adding an alternative API? W/D/T? |
Hey @jquense thanks for your response.
yes, this is mainly the issue I was trying to solve but leaving it more open to solve other problems (eg accessing ancestors/cousins, but thats not my main goal)
That would be good, ideally whatever solution we end up with we can use it together with other yup ecosystem (other schemas validation, yup.ref, etc). Do you have any ideas on alternatives implementation? Has there been any proposed solutions in the past? |
we added the |
we tried the from property but found it not very intuitive
number().when(['$root', '$idx'], ([root, idx], schema) =>
idx > 0 ? schema.moreThan(root[idx - 1]) : schema,
), |
Yes thats what i'm suggesting changing,
Not at the moment, but it could be via refs
I think this is a distinct and separate feature from accessing root properties, I would like to consider it separately if that's ok |
Makes sense, I'll separate the |
This is a proof of concept to give more access in the schema to the parent/any element in the schema. Adds two new methods:
schema.asContext
Adds the whole value as a context, this allows to access any random property of the schema from any node or sub property.
Eg
array.indexContextKey
Adds the current element index as a context value, this is useful when comparing the element to previous elements in the array
Eg to make sure array is sorted