
Typyn Documentation
๐ Lightweight, Chainable TypeScript Validation
Validate data with speed and confidence โ zero dependencies, full type inference.
Introduction โ
Typyn is a minimalist validation library designed for TypeScript developers who need fast, intuitive schema building without the bloat. Drawing inspiration from tools like Zod and Yup, but stripped down for performance (up to 10M+ ops/sec on primitives), Typyn lets you chain rules fluently while ensuring runtime safety and compile-time types.
๐ Quick Start โ
Installation โ
Install Typyn using your preferred package manager:
# Using pnpm (recommended)
pnpm add typyn
# npm
npm install typyn
# yarn
yarn add typyn
# bun
bun add typynBasic Usage โ
Create a simple schema with the v factory:
import { v } from "typyn";
// Define a schema
const userSchema = v.object({
name: v.string().min(2).max(50),
age: v.number().min(18).default(18),
email: v.string().email().optional(),
});
// Validate input (throws on error)
const user = userSchema.parse({ name: "Alice", age: 22 });
console.log(user);
// โ { name: "Alice", age: 22 }
// Safe validation (no throws)
const result = userSchema.safe({ name: "A", age: 10 });
if (!result.success) {
console.error(result.error.message);
// โ "name โ Must be at least 2 characters"
}๐งฉ Extra Features โ
Typyn schemas are chainable, type-safe, and composable:
// Add transforms or defaults
const emailSchema = v.string().email().trim().transform(e => e.toLowerCase());
// Use refinements for custom logic
const even = v.number().refine(n => n % 2 === 0, "Must be even");
// Combine schemas
const config = v.object({
port: v.number().min(1000).max(9999),
secure: v.boolean().default(false),
});โ
.parse() โ strict mode (throws on failure). โ
.safe() โ safe mode (returns { success, data?, error? }). โ
.default() โ provides fallback values. โ
.transform() โ modify output post-validation.
