I'm excited to announce banditypes — a new validation library for TS and JS. You may have heard of zod or superstruct — these great tools let you define a JS schema to validate objects, and automatically convert the schema to a TS type to avoid duplication. Well, banditypes does the same thing, but in 400 bytes!
Curious to learn how it can be so small? It boils down to three design principles:
- Strict scope cutting. More full-featured validation libraries tell you what, exactly, failed validation. This feature had to go.
- Export functions, not objects with methods, because functions tree-shake better. This is exactly why superstruct is already 3–5 times lighter than zod.
- More extensibiltiy. If you cut features from the core, the users must be able to write the missing bits themselves.
And some more optimization on top:
- Compile to modern JS. Gzip is good, but replacing
function ()
with() =>
still saved 20 bytes! - Minimal API. Bye-bye
literal(42)
, we haveenums([42])
- Gzip-aware code style. Gzip removes repetition, so you need to make different parts of code as similar as possible. Write
Object
once, repeat for free. Surprise: copy-pasting functions with minor changes is often smaller under gzip than proper code reuse.
I have published an article with a more in-depth explanation.
Banditypes is not a validation library for every use case, but it gains something from its minimal design, and was fun to build. Check it out on GitHub, and maybe even give it a spin in your next project!
Top comments (0)