-
Notifications
You must be signed in to change notification settings - Fork 935
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
Similar keys in object schema causing field to use the wrong validation #2252
Comments
@jquense Just checking in, as I haven't heard anything about this submission/bug. |
@webdevnerdstuff The issue does not seem to be with yup Library. try the code once from attachments @webdevnerdstuff Platform: I have executed in the same stackblitz env that was shared. Yup Version ^1.4.0 |
@karthikMNK please format your code properly. Regardless I used your example, and it's not correct. @jquense is this project dead? |
I can't provide support for form integrations i don't own, and I can't reproduce the problem with just a yup schema, I also don't know Vue so the example is difficult for me to even evaluate. If you could want to provide a minimal runnable example of where yup is clearly doing the wrong thing it's more likely i can quickly provide help |
Here is the same example, but excluding the |
In the vanilla example you are validating the entire object and every key of Run: schema.validate(modelValue.value, { abortEarly: false }) and you'll see that you get all 3 errors. |
So when If I change the order of the schema by switching const schema = object({
fooBar: string().required(isRequired('Foo Bar')),
foo: string().required(isRequired('Foo')),
fooBiz: string().required(isRequired('Foo Biz')),
}); When |
The current abortEarly is working at Individual Field level
for a string if we configure required and length both .
yup aborts at required when abortEarly=true else show both errors
The current work around solution is discussed in this link
reactjs - How to get first error while using yup to validate object schema
- Stack Overflow
<https://stackoverflow.com/questions/67724034/how-to-get-first-error-while-using-yup-to-validate-object-schema>
Its nice to have abortFieldEarly,abortSchemaEarly as separate fetaures.
…On Fri, Jan 24, 2025 at 4:29 AM WebDevNerdStuff ***@***.***> wrote:
So when abortEarly is true, it will stop on the last invalid field, not
the first one it encounters? Shouldn't it stop on the first invalid field,
not the last if it's set to true? Or is the order of the schema reversed
when it's actually validating?
If I change the order of the schema by switching foo and fooBar, it does
the same, it stops at foo instead of fooBar.
const schema = object({
fooBar: string().required(isRequired('Foo Bar')),
foo: string().required(isRequired('Foo')),
fooBiz: string().required(isRequired('Foo Biz')),});
When abortEarly is false, it is checking all fields, and it does get that
result of all 3 errors correctly like you suggested.
—
Reply to this email directly, view it on GitHub
<#2252 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/BIN4MU3OZJV3ICAOZGTJIA32MFX4TAVCNFSM6AAAAABQPV5GH6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMMJRGE4DMNJRGI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
abortEarly just returns the first thing that resolves to an error, what and which isn't specifically deterministic since it's whatever validation promise rejects "fastest". Yup doesn't make any attempt to be smart about this when everything resolves at the same time, so you get whatever happens naturally based on iteration orders internally and whatever the browser wants to report first |
I think I got distracted from the main issue. Normally it does validate in the correct order that is set, and everything works perfectly. This issue is when there are similar keys that start the same when things are off. What seems to be happening is that So for this example: const schema = object({
foo: string().required(isRequired('Foo')),
fooBar: string().required(isRequired('Foo Bar')),
fooBiz: string().required(isRequired('Foo Biz')),
}); Technically const schema = object({
fooBar: string().required(isRequired('Foo Bar')),
foo: string().required(isRequired('Foo')),
fooBiz: string().required(isRequired('Foo Biz')),
}); If the fields/validations are the same, and whatever validation promise rejects "fastest", wouldn't it make sense that the same field would error first, and the order in the schema not matter? it's aborting on the last error it runs into in the order that is set in the schema, or it's running the validation starting at the end of the schema and working it's way up, which would then make sense if it's aborting early. Or is it because I'm not using async? I do like the idea that @karthikMNK mentioned of having a |
trying to make the validation order consistent is not the right solution for your problem. The underlying issue is that a form can't validate the entire schema when updating a single field expect it to know which of the errors is most relevant. The form library should be surgical about what field it validates and when, OR it should expose all the errors and let you decide which one you want to surface to the user. |
It does validate in the order of the schema from what I can tell. Where it doesn't is specifically when a key is similar like in the examples, and in that case it acts in a reverse manner. Is that just a coincidence it works in schema order when all keys are unique? |
i am not sure honestly, you can look at https://github.com/jquense/yup/blob/master/src/util/sortByKeyOrder.ts the |
Describe the bug
When there are similar key's in the validation schema, it is causing it to not use the incorrect validation. I think this is a
yup
issue not avee-validate
issue?To Reproduce
fooBar
validation notfoo
fooBiz
validation notfoo
ex. schema
Expected behavior
It should use the correct validation set.
Platform (please complete the following information):
^1.4.0
^4.14.4
The text was updated successfully, but these errors were encountered: