[INTERNAL CHANGE] Built-in support for multiple validation libraries #201
Pinned
TheEdoRan
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Starting from version 7.2.0, next-safe-action ships with a built-in system for supporting multiple validation libraries. At the time of this writing, the library supports Zod (default), Valibot and Yup. The support may be extended to other libraries in the future.
Have no time to read? Skip to the migration guide.
Why?
next-safe-action v6 enabled support for validation libraries other than Zod thanks to the amazing TypeSchema lib. Same thing for v7, obviously. Unfortunately, though, the dynamic import strategy that TypeSchema uses, plus TypeScript 5.5 release have caused some issues for next-safe-action users:
validate
function breaks Next.js edge runtime decs/typeschema#54For this reason, starting from v7.2.0, next-safe-action ships with a built-in system that enables support for 3 validation libraries (at this time). More information below.
Migration guide
Not much to do, really! If you use Zod, you don't have to do anything. If you use Valibot or Yup, you should uninstall
@typeschema/valibot
or@typeschema/yup
(just the TypeSchema adapters, not the libraries themselves) and then update the initialization of the safe action client:If you want to use Yup instead, replace all occurrences of "valibot" with "yup" in the above example and you're done.
Quick note:
zodAdapter
is the default one, so you don't need to import and pass it to thevalidationAdapter
prop. This is because Zod is by far the most used library with next-safe-action (1, 2) and I wanted to make the upgrade to v7.2.0 to be as smooth as possible. I still have to decide if thevalidationAdapter
prop should be optional (as it is right now) or not, to make the library truly independent from the under the hood validation library that a user decides to use, but this approach seemed to be the most appropriate at this time.Conclusion
Hopefully, you have now understood why I needed to make this internal change to the library. The upgrade shouldn't be really a PITA for you end users, so that's good. If you used a library supported by TypeSchema that's not currently included with the new system, feel free to open a discussion to request the support for it. If there will be interest for that, I'll consider including it with an update.
Having said all that, you can now use next-safe-action with your validation library of choice, also with the Edge Runtime and TypeScript >= 5.5, hopefully with no problems whatsoever.
And as a last note, I really want to thank all the TypeSchema contributors for building such an amazing library that highly inspired the creation and implementation of this built-in system. Without your work, this would have been much more difficult to write, so thank you!
Enjoy next-safe-action v7.2.0!
Beta Was this translation helpful? Give feedback.
All reactions