DEV Community

Avinash
Avinash

Posted on

valicore: Runtime Type Validation for TypeScript (Schemas, Guards, Parsing)

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
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Type Guards

if (UserSchema.is(data)) {
  // data is narrowed to User type here
  sendWelcomeEmail(data.email);
}
Enter fullscreen mode Exit fullscreen mode

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()
});
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)