DEV Community

Cover image for Meet banditypes — the smallest TypeScript validation library
Vladimir Klepov
Vladimir Klepov

Posted on

1

Meet banditypes — the smallest TypeScript validation library

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!

A code sample of banditypes validator

Curious to learn how it can be so small? It boils down to three design principles:

  1. Strict scope cutting. More full-featured validation libraries tell you what, exactly, failed validation. This feature had to go.
  2. Export functions, not objects with methods, because functions tree-shake better. This is exactly why superstruct is already 3–5 times lighter than zod.
  3. 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:

  1. Compile to modern JS. Gzip is good, but replacing function () with () => still saved 20 bytes!
  2. Minimal API. Bye-bye literal(42), we have enums([42])
  3. 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!

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay