A few weeks ago I released a TypeScript library called @traversable/valibot.
This post covers what the library does, and highlights a few of its unique features.
Cover photo credit:
Astro Boy Schematics (鉄腕アトム), Tezuka ProductionsNote:
Currently @traversable/valibot only works with the latest version of Valibot (v1.1).
What makes @traversable/valibot different?
@traversable/valibot can be used in 2 ways:
- Pick one of over 15 transformers available off-the-shelf
- Hand-roll your own custom transformer
Off-the-shelf
ᯓ🚀 vx.check
Turn a Valibot schema into a super-performant type-guard
ᯓ🚀 vx.deepClone
Converts a Valibot schema into a “deep copy” function
ᯓ🚀 vx.deepEqual
Converts a Valibot schema into a “deep equal” function
ᯓ🚀 vx.toString
Converts a Valibot schema into a string
Useful for testing, codegen, sanity checking
ᯓ🚀 vx.toType
Converts a Valibot schema into a TypeScript type
🏷️ Preserves schema metadata as JSDoc annotations
ᯓ🚀 vx.defaultValue
✨new!✨
Derive a configurable default value from a schema
Designed for schema-driven forms on the frontend
ᯓ🚀 vx.makeLens
✨coming soon!✨
🔍 Focus nested values with lenses (getters/setters)
🌈 Reify control flow with prisms (pattern-matchers)
⛰️ Map over containers with traversals (for-loops)
🧱 Stack them together with function composition
Roll your own
ᯓ🚀 vx.fold
All the off-the-shelf transformers that @traversable/valibot ships are powered by vx.fold.
Under the hood, the library uses an abstraction called recursion schemes that make implementing recursion simple and fun.
To demonstrate, I used it to implement a mock data generator.
The implementation is so minimal (25 lines!) that I've embedded it here, in its entirety:
Note:
To keep things simple, this is a partial implementation.
For a more robust version, see this StackBlitz.
Bonus: @traversable/valibot-test
@traversable/valibot-test is a testing library built specifically for library authors who are building on top of Valibot.
Under the hood, it uses a TypeScript library called fast-check to generate random Valibot schemas.
Being able to generate random schemas turns out to be incredibly useful for fuzz testing.
For example, here we use it to test that our valibotToFaker
function always generates valid data:
💡 Tip:
Scroll up in the terminal panel to see the generated schemas + mock data!
Updates
Update: as of yesterday, @traversable/valibot is part of the Valibot ecosystem!
doc: adds `@traversable/valibot` and `@traversable/valibot-test` to ecosystem
#1298
Hello Valibot maintainers!
This PR adds 2 libraries to the ecosystem:
These libraries were primarily built for developers who are building tools on top of Valibot, but can also be used for one of the 10+ off-the-shelf transformers that we currently support.
Happy to talk more about the library if you have any questions!
Comment for
#1298
Thank you! And sorry for the late reaction! What you have built looks amazing! Should we do a post on social media?
The Valibot ecosystem is by far the most welcoming and collaborative TypeScript schema community out there, so I'm super proud to be able to contribute back ☺️.
Closing thoughts
Thanks for reading!
If you have any feedback, feature requests, or questions, feel free to open an issue or start a discussion.
Links
- @traversable/valibot on GitHub
- valibotToFaker demo on StackBlitz
- Fully-fledged valibotToFaker demo on StackBlitz
Top comments (4)
Article steals focus. Not very accessible
I tried! Do you know if dev.to supports a way to inject JavaScript? I could maybe prevent it from stealing focus again
Great article 🚀
Thanks for reading! 😍