A hands-on showcase of modern data validation approaches in Node.js, covering Zod, Joi, Valibot, etc and best practices.
  • TypeScript 82.6%
  • Shell 14.4%
  • JavaScript 3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-04-12 23:06:26 +05:30
bench feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
scripts feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
src feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
.gitignore chore(gitignore): update gitignore 2026-04-12 23:06:26 +05:30
.nvmrc feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
.swcrc feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
LICENSE.md chore(license): add MIT license 2026-04-11 21:03:00 +05:30
package.json feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
pnpm-lock.yaml feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
README.md feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
rspack.config.js feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30
tsconfig.json feat(validation): implement advanced patterns and performance benchmarking suite 2026-04-12 22:48:03 +05:30

🛡️ Node.js Data Validation & High-Performance Tooling Playground

A technical exploration into modern Type-Safe Data Validation and the High-Performance Tooling (Go & Rust) ecosystem. This project analyzes the intersection of Runtime Safety, Developer Experience (DX), and Production Performance (Bundle Size/Tree-shaking).


🏛️ Architectural Concepts

1. Validation as a Boundary ("Parse, Don't Validate")

This project implements the "Parse, Don't Validate" philosophy: data is not just checked for correctness but is transformed into trusted internal types at the application boundary (API Gateway, Database Layer).

2. Schema-Driven Type Inference

We prioritize libraries that support Automatic Type Inference. By defining a schema once, both the runtime validator and the TypeScript interfaces are derived, eliminating manual interface drift and ensuring synchronization between code and data.


📊 Benchmarks & Comparative Analysis

The following data represents the "bundle tax" of adding validation to an application, measured using esbuild (Minified + Gzipped).

Library Bundle Size (scripts/bundle-size.sh)

Library Minified Gzipped Verdict
Valibot 4.0K 1.6K Best for Edge/Web. Modular, functional API allows for aggressive tree-shaking.
Ajv 114K 35K Best for Backend. High performance via JSON Schema pre-compilation.
Joi 174K 56K Node-Specific. Powerful declarative API, but lacks native TS inference.
Zod 304K 61K Industry Standard. Comprehensive feature set and first-class TS integration.

🛠️ Tooling Comparison: Go (esbuild) vs. Rust (Rspack)

Tool Ecosystem Language Core Strength
esbuild Bundler/Minifier Go Speed-focused, zero-config bundling.
SWC Transpiler Rust High-speed single-file transformations (Babel replacement).
Rspack Optimized Bundler Rust Webpack compatibility with Rust-based performance.

🔬 Insight: Tree-Shaking Efficiency

During benchmarking, a significant difference was observed in how Rspack (Rust) and esbuild (Go) handle the zod library:

  • esbuild output: ~304KB
  • Rspack output: ~97KB Conclusion: Rspack's dependency graph analysis is more effective at pruning unused code from monolithic libraries like Zod compared to esbuild's simpler, faster approach.

📚 Implementation Patterns

The src/ directory contains implementations of several validation patterns:

  1. src/01-parse-dont-validate.ts: Data transformation (e.g., strings to Date objects) during the parsing phase.
  2. src/06-api-gateway.ts: Discriminated Unions and RFC 7807 (Problem Details) for standardized, machine-readable error responses.
  3. src/07-branded-types.ts: Branded Types to prevent "Primitive Obsession" (e.g., preventing a UserId from being used where an OrderId is expected).
  4. src/08-recursive-schemas.ts: Handling self-referential structures (like categories or file systems) with z.lazy().

🚀 Usage

Installation

pnpm install

Running Benchmarks

pnpm run bench:size     # Analyze library bundle sizes
pnpm run bench:tools    # Compare esbuild (Go) vs Rspack (Rust)

Running Demos

pnpm run demo:parse     # Parse, Don't Validate + Transformation
pnpm run demo:branded   # Branded Types (Domain Safety)
pnpm run demo:api       # RFC 7807 Standardized Errors
pnpm run demo:recursive # Recursive Schema Handling

🏁 Summary

For client-side and edge environments where bundle size is critical, Valibot is the recommended choice. For backend-heavy TypeScript projects requiring the best DX and feature set, Zod paired with Rspack provides the optimal balance of type safety and bundle efficiency.