Package Exports
- dhi
- dhi/dhi.wasm
- dhi/schema
- dhi/schema-nextjs
- dhi/turbo
Readme
dhi - Ultra-Fast Validation for JavaScript/TypeScript
1.64x faster than Zod with 40.26M ops/sec in TURBO mode! ๐
Drop-in replacement for Zod with WASM-powered performance.
Quick Start
npm install dhi
# or
bun add dhiBasic Usage (Drop-in Zod Replacement)
import { z } from "dhi/schema";
// Works exactly like Zod!
const UserSchema = z.object({
name: z.string().min(2).max(100),
email: z.string().email(),
age: z.number().positive().int(),
role: z.enum(["admin", "user", "guest"]),
tags: z.array(z.string()).optional()
});
// Validate single item
const user = UserSchema.parse({ name: "Alice", email: "alice@example.com", age: 30, role: "user" });
// Safe validation
const result = UserSchema.safeParse(data);
if (result.success) {
console.log(result.data);
} else {
console.log(result.error);
}TURBO Mode (Maximum Performance)
For simple schemas with string length and number range validations:
import { turbo } from "dhi/turbo";
// 40.26M ops/sec!
const schema = turbo.object({
name: turbo.string(2, 100),
age: turbo.number(18, 120)
});
// Validate thousands at once
const users = [/* ... 100K users ... */];
const results = schema.validateMany(users);Batch API (8.19x faster on mixed data)
import dhi from "dhi";
const schema = {
name: dhi.z.string(2, 100),
email: dhi.z.email(),
age: dhi.z.positive()
};
// Blazing fast on mixed valid/invalid data
const results = dhi.validateBatch(users, schema);Performance
| Mode | ops/sec | vs Zod | Best For |
|---|---|---|---|
| TURBO | 40.26M | 1.64x faster ๐ฅ | Simple schemas, maximum speed |
| Batch (mixed data) | 15.76M | 8.19x faster ๐ฅ | Real-world data with errors |
| Feature-complete | 7.14M | 0.66x | Full Zod compatibility |
Features
All Zod Features โ
String Validators
min(),max(),length()- Length constraintsemail(),url(),uuid()- Format validationstartsWith(),endsWith(),includes()- String checksregex()- Custom patternstrim(),lowercase(),uppercase()- Transformations
Number Validators
min(),max()- Rangegt(),gte(),lt(),lte()- Comparisonspositive(),negative(),nonnegative()- Sign checksint(),finite()- Type constraintsmultipleOf()- Divisibility
Composite Types
object()- Object schemasarray()- Array validationunion()- Multiple typesenum()- Enumerationsoptional(),nullable()- Modifiers
Advanced
.transform()- Data transformation.refine()- Custom validation.default()- Default values- Type inference with
z.infer<>
API Comparison
dhi (Drop-in Replacement)
import { z } from "dhi/schema";
// Works exactly like Zod!
const schema = z.object({
name: z.string().email(),
age: z.number().positive()
});Zod
import { z } from "zod";
const schema = z.object({
name: z.string().email(),
age: z.number().positive()
});Yes, it's that simple! Just change the import and you're done!
Migration from Zod
Option 1: Alias (Quickest)
// Old: import { z } from "zod";
import { z } from "dhi/schema";
// Everything else stays the same!Option 2: Gradual Migration
// Keep using Zod where needed
import { z as zodz } from "zod";
// Use dhi for performance-critical paths
import { z } from "dhi/schema";
import { turbo } from "dhi/turbo";When to Use Each API
Use TURBO Mode When:
- โ Simple schemas (string length, number range)
- โ Validating thousands of items
- โ Maximum performance needed
- โ Production workloads
Use Batch API When:
- โ Mix of valid and invalid data
- โ Need early-exit optimization
- โ Real-world scenarios
Use Feature-Complete API When:
- โ Need full Zod compatibility
- โ Complex schemas with email, URL, UUID
- โ Transformations and refinements
- โ Detailed error messages
Real-World Example
import { z } from "dhi/schema";
// Financial data validation
const TradeSchema = z.object({
tradeId: z.string().min(10).max(50),
cusip: z.string().length(9),
quantity: z.number().positive().int(),
price: z.number().positive(),
settlementDate: z.string(),
counterparty: z.string().min(5)
});
// Validate 100K trades
const trades = [/* ... */];
const results = trades.map(t => TradeSchema.safeParse(t));
// Or use batch mode for even more speed
import dhi from "dhi";
const batchResults = dhi.validateBatch(trades, {
tradeId: dhi.z.string(10, 50),
cusip: dhi.z.string(9, 9),
quantity: dhi.z.positive(),
price: dhi.z.positive(),
settlementDate: dhi.z.isoDate(),
counterparty: dhi.z.string(5, 100)
});Bundle Size
- WASM module: 9.2KB (smaller than most validators!)
- Tree-shakeable
- Zero dependencies (WASM is included)
Browser Support
Works everywhere that supports WASM:
- โ Chrome/Edge 57+
- โ Firefox 52+
- โ Safari 11+
- โ Node.js 18+
- โ Deno
- โ Bun
TypeScript Support
Full TypeScript support with type inference:
import { z, infer as zodInfer } from "dhi/schema";
const UserSchema = z.object({
name: z.string(),
age: z.number()
});
type User = zodInfer<typeof UserSchema>;
// { name: string; age: number }Benchmarks
Run benchmarks yourself:
git clone https://github.com/justrach/satya-zig.git
cd satya-zig/js-bindings
bun install
bun run benchmark-final.tsWhy dhi?
- ๐ Blazing Fast: 1.64x-8.19x faster than Zod
- โ Zod Compatible: Drop-in replacement
- ๐ฏ Three APIs: Choose speed vs features
- ๐ฆ Tiny: 9.2KB WASM
- ๐ Universal: Works everywhere
- ๐ Type-Safe: Full TypeScript support
License
MIT
Links
Made with Zig + WASM | เคงเฅ means wisdom/intellect in Sanskrit ๐ง