TypeScript is great for catching type errors at compile time — but what about runtime? External API responses, form data, env vars — they're all untyped at runtime. valicore fixes that.
What is valicore?
A zero-dependency TypeScript validation library with full schema inference, type guards, and safe parsing.
npm install valicore
bun add valicore
Core Features
Schema Definition
import { v } from 'valicore';
const UserSchema = v.object({
id: v.number(),
name: v.string().min(1).max(100),
email: v.string().email(),
role: v.enum(['admin', 'user', 'guest']),
createdAt: v.date().optional()
});
type User = v.infer<typeof UserSchema>;
// Automatically inferred TypeScript type!
Safe Parsing
const result = UserSchema.safeParse(apiResponse);
if (result.success) {
console.log(result.data.email); // Fully typed!
} else {
console.error(result.errors); // Detailed error messages
}
Type Guards
if (UserSchema.is(data)) {
// data is narrowed to User type here
sendWelcomeEmail(data.email);
}
Nested Schemas
const OrderSchema = v.object({
id: v.string().uuid(),
user: UserSchema,
items: v.array(v.object({
productId: v.string(),
quantity: v.number().int().positive()
})),
total: v.number().positive()
});
Why valicore?
- Zero dependencies
- Automatic TypeScript type inference
- Detailed, human-readable error messages
- Composable and reusable schemas
- Works with Node.js, Bun, Deno, and browsers
Install
npm install valicore
GitHub: https://github.com/Avinashvelu03/valicore
How do you handle runtime validation in your TypeScript projects? Drop your approach in the comments!
Top comments (0)