DEV Community

Cover image for Introducing: @traversable/valibot
andrew jarrett
andrew jarrett

Posted on

Introducing: @traversable/valibot

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 Productions

Note:
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:

  1. Pick one of over 15 transformers available off-the-shelf
  2. Hand-roll your own custom transformer

Off-the-shelf

ᯓ🚀 vx.check

ᯓ🚀 vx.deepClone

ᯓ🚀 vx.deepEqual

ᯓ🚀 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.defaultValuenew!

  • Derive a configurable default value from a schema

  • Designed for schema-driven forms on the frontend

ᯓ🚀 vx.makeLenscoming 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!

@traversable/valibot-test demo


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:

  1. @traversable/valibot
  2. @traversable/valibot-test

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

  1. @traversable/valibot on GitHub
  2. valibotToFaker demo on StackBlitz
  3. Fully-fledged valibotToFaker demo on StackBlitz

Top comments (4)

Collapse
 
one_cat profile image
Katerina

Article steals focus. Not very accessible

Collapse
 
ahrjarrett profile image
andrew jarrett

I tried! Do you know if dev.to supports a way to inject JavaScript? I could maybe prevent it from stealing focus again

Collapse
 
mjh_biz profile image
Mariana Hernandez

Great article 🚀

Collapse
 
ahrjarrett profile image
andrew jarrett

Thanks for reading! 😍