zod-validation-error: TypeError: Cannot read properties of undefined (reading 'slice') at fromZodError
I’m facing this issue. How do we solve this?
TypeError: Cannot read properties of undefined (reading 'slice')
at fromZodError (/Users/abdulghani/Documents/projects/office/wayship-ahoy-client/node_modules/zod-validation-error/dist/cjs/ValidationError.js:71:10)
```
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 18 (9 by maintainers)
The
toValidationErrorhas been added to the docs for a while now. https://github.com/causaly/zod-validation-error?tab=readme-ov-file#tovalidationerrorPoint taken that we need to do a better job describing the method.
I am strongly opinionated here against OOP. I personally see no benefit of having it as a static method as part of
ValidationError. Thanks for the suggestion though.Honestly? I didn’t even see
toValidationError, likely given the fact that it’s not on the npm page, or noted anywhere in the documentation. This does 100% solve the issue I have, however, the lack of any real documentation indicating that this method should be used over thefromZodError, or that it even exists. I would almost suggest the removal offromZodErrorcompletely and instead recommend that everything go throughtoValidationError, but you guys havefromZodIssue, which is helpful as well, and not used intoValidationErrorat all.If it were me, I’d remove all the
from*functions (I realize this is a breaking change and therefore wouldn’t just happen in the next release) and instead move to a singulartoValidationErrorwhich keeps the current logic, while also adding in logic to convertZodIssues.Sidebar, any particular reason for
toValidationErrorbeing a standalone function, rather than a static based method in theValidationErrorclass? Perhaps this is my OOP bias showing, but it seems like it belongs there…Oh, I absolutely feel like it is fair to say that it is in the user consumer to make sure they are using this package correctly and not passing in things they shouldn’t, I simply mean that by allowing this function to error when trying to call
sliceonundefined, as it does currently, gives off the impression that it is a bug, as based off the documentation I would expect aTypeErrortelling me that I passed an invalid argument, rather than the current behavior. It also seems somewhat against the original intention of this package, which was making it easier to use the error details returned by Zod. Seems a little counterproductive to use the current error as a means of saying you’ve passed the wrong value, no?Regardless, I’d be interested in what others have to say. I don’t think it’s a huge deal either way, given it is rather easy to validate the error is a
ZodErroror not before hand.@unRealGamer28 I understand your concern, although your test script above does only one thing, which is an object parsing using a zod schema. But what if in a real-world scenario your function did many things? Like http requests, IO operations plus zod schema parsing. Then you might end up with all sorts of different error instances.
So the point I’m trying to make is that it is almost inevitable to do a check against the instance of the error you are getting in the catch block. But there might be another discussion here, whether we could entertain the idea of adding an extra helper in the library, something like
isZodError.cc @jmike @nikoskalogridis what are your thoughts on this?