Zero-dependency TypeScript validation library with click-to-jump debugging (source location tracking). Supports custom rules & nested objects.
npm install tysctysc is a zero-dependency validation library that combines the convenience of Zod with the speed of JIT compilation.
assert() and check() to validate raw JSON directly. No need to manually instantiate classes.
check(User, body) validates the object and narrows TypeScript type, but does not remove unknown properties.
bash
npm install tysc
`
---
β‘Quick Start
$3
`ts
import { IsString, IsNumber, Min, Max } from "tysc";
class CreateUserDto {
@IsString({ message: "Username is required" })
name!: string;
@IsNumber()
@Min(18)
age!: number;
}
`
$3
Use assert to validate, strip unknown keys, and transform JSON in one go.
`ts
import { assert, ValidationException } from "tysc";
// Incoming JSON request (unknown type)
const body = { name: "Alice", age: 25, admin: true };
try {
// 1. Validates data
// 2. Returns a typed instance of CreateUserDto
// 3. Optionally strips unknown properties if { stripUnknown: true } is passed
const user = assert(CreateUserDto, body, { stripUnknown: true });
console.log(user.name); // Typed as string!
console.log(user); // CreateUserDto { name: "Alice", age: 25 }
} catch (e) {
if (e instanceof ValidationException) {
console.error(e.errors); // Detailed error array
}
}
`
$3
Use check for simple boolean checks. It uses abortEarly internally for maximum speed.
`ts
import { check } from "tysc";
if (check(CreateUserDto, body)) {
// TypeScript now knows 'body' is CreateUserDto
console.log(body.age);
}
`
---
β‘ Performance Benchmark
Benchmark conducted on v2.5.0 (1,000,000 iterations).
With Stack-based Optimization and Zero-Allocation architecture, tysc dominates in complex scenarios.
$3
> Deeply nested objects + Arrays + Recursion.
| Library | Ops/Sec | Relative Speed | Note |
| :-------------- | :-----------: | :----------------: | :-----------------: |
| tysc π | 6,188,961 | 100% (Fastest) | Zero-Allocation |
| zod | 4,572,810 | 73.9% | Functional |
| class-validator | 266,609 | 4.3% | OOP |
> π‘ Insight:
> For complex data structures, tysc is 35% faster than Zod and ~23x faster than class-validator.
---
$3
| Library | Ops/Sec | Relative Speed |
| :-------------- | :------------: | :------------: |
| zod | 25,516,518 | 100% |
| tysc π | 20,678,246 | 81.0% |
| class-validator | 1,018,098 | 4.0% |
> π‘ Note: Even in simple scenarios, tysc processes over 20 million ops/sec, making it 20x faster than class-validator.
---
βοΈ Advanced Features
$3
Stop validation immediately after finding the first error. Useful for saving resources on large datasets.
`ts
const errors = validate(obj, { abortEarly: true });
`
$3
Secure your API by automatically removing fields that are not decorated in your DTO.
`ts
const body = { name: "Alice", age: 25, admin: true };
const user = assert(CreateUserDto, body, { stripUnknown: true });
`
$3
Validate nested objects and arrays of objects easily.
`ts
class Post {
@IsString()
title!: string;
}
class User {
@IsArray()
@ValidateNested()
posts!: Post[];
}
`
$3
Register your own high-performance validation logic using registerStrategy.
`ts
import { createDecorator, registerStrategy } from "tysc";
// 1. Register Logic
registerStrategy("IsKoreanPhone", (val, rule, prop) => {
return /^010-\d{4}-\d{4}$/.test(val) ? null : "Invalid format";
});
// 2. Create Decorator
function IsKoreanPhone() {
return createDecorator("IsKoreanPhone");
}
`
π API Reference
$3
assert(Class, json, options?): Validates JSON and returns an instance. Throws ValidationException on failure.
check(Class, json): Returns true if valid. Acts as a Type Guard.
validate(instance, options?): (Traditional) Validates an existing class instance. Returns ValidationError[].
$3
Common: @IsString, @IsNumber, @IsBoolean, @IsOptional
String: @IsEmail, @Length(min, max), @Matches(regex)
Numeric: @Min(n), @Max(n), @IsInt, @IsPositive
Array/Nested: @IsArray, @ValidateNested, @ArrayMinSize, @ArrayMaxSize
β οΈ Configuration
To use decorators, you must enable the following settings in your tsconfig.json:
`JSON
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
``