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

impossible to set setFieldError on array fields with latest toTypedSchema #4211

Closed
5 tasks done
hi-reeve opened this issue Apr 11, 2023 · 11 comments · Fixed by #4222
Closed
5 tasks done

impossible to set setFieldError on array fields with latest toTypedSchema #4211

hi-reeve opened this issue Apr 11, 2023 · 11 comments · Fixed by #4222
Labels
🐛 bug Unintended behavior

Comments

@hi-reeve
Copy link
Contributor

What happened?

from this issue

we can setFieldError on useFieldArray like this setFieldError('users[0].name','error message')
but with the latest toTypedSchema the autocomplete of setFieldError is only reading the root property of the validationSchema

Reproduction steps

  1. create a validationSchema with array type
  2. try to manually set the object of the array type schema ex: users[0].name
  3. typescript will yell at you
    ...

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

-

Demo link

https://stackblitz.com/edit/vitejs-vite-1kvon3?file=src/App.vue

Code of Conduct

@logaretm logaretm added the ✨ enhancement a "nice to have" feature label Apr 14, 2023
@logaretm
Copy link
Owner

logaretm commented Apr 14, 2023

This is tricky to have since it means we need to have the field paths as recursive TS literal type instead of keys of the given object.

But I believe it is a worthwhile enhancement and will remove all weirdness about setting fields and errors values for nested paths.

I will be working on this for the next minor release.

@hi-reeve
Copy link
Contributor Author

This is tricky to have since it means we need to have the field paths as recursive TS literal type instead of keys of the given object.

But I believe it is a worthwhile enhancement and will remove all weirdness about setting fields and errors values for nested paths.

I will be working on this for the next minor release.

thanks!
right now my workaround is only to do @ts-expect-error directive but i believe this is not a good solution

@hi-reeve
Copy link
Contributor Author

hi-reeve commented Apr 18, 2023

stumble upon this today, this also will be a problem if the key of the validation schema is dynamic and not defined before.

@hi-reeve
Copy link
Contributor Author

@logaretm this breaks in the new version. the value is set to array.index.key not to the correct index

@logaretm
Copy link
Owner

@hi-reeve What do you mean? Don't think something was changed related to this since the fix. Got an example?

@hi-reeve
Copy link
Contributor Author

hi-reeve commented Jul 6, 2023

@hi-reeve What do you mean? Don't think something was changed related to this since the fix. Got an example?

take a look at the current version
https://stackblitz.com/edit/vitejs-vite-ynk5jg?file=src%2FApp.vue

when setting setFieldError using the supposed to be method field.${idx}.property it doesn't correctly set the error for the field

and if we set the name of the field to be field.${idx}.property
doesn't seem the validation is working either

the vue devtools detecting if we name the field format as field.${idx}.property is not the same as field[${idx}].property isn't this supposed to be the same? it read the same on version 4.9.x

image

so with this, if we set the error using the format field.idx.property the validation will always be false

@hi-reeve
Copy link
Contributor Author

hi-reeve commented Jul 7, 2023

@logaretm

@logaretm
Copy link
Owner

logaretm commented Jul 7, 2023

Right, seems like paths should be normalized to use one syntax over the other. I will try to look into what we need to do here.

@logaretm logaretm reopened this Jul 7, 2023
@logaretm logaretm added 🐛 bug Unintended behavior and removed ✨ enhancement a "nice to have" feature labels Jul 7, 2023
@hi-reeve
Copy link
Contributor Author

hi-reeve commented Jul 8, 2023

Right, seems like paths should be normalized to use one syntax over the other. I will try to look into what we need to do here.

thankyou. and regarding setting this field to field.idx.property yup does not read this as an array validation but a normal object because the output was field.0.property not field[0].property

@logaretm
Copy link
Owner

logaretm commented Jul 8, 2023

Right, that shouldn't be an issue with the fix since .0. and [0] are exactly the same thing to vee-validate. I realize that means objects with 1 or numeric keys are not possible but that was always intended, this may change in v5 but for now both should refer to the same path.

There might be some bugs around this area but thanks to you reporting this, it should be more consistent now. I will tag a release shortly with the fix.

@hi-reeve
Copy link
Contributor Author

Right, that shouldn't be an issue with the fix since .0. and [0] are exactly the same thing to vee-validate. I realize that means objects with 1 or numeric keys are not possible but that was always intended, this may change in v5 but for now both should refer to the same path.

There might be some bugs around this area but thanks to you reporting this, it should be more consistent now. I will tag a release shortly with the fix.

thankyou for the quick fix! great job!

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

Successfully merging a pull request may close this issue.

2 participants