Introduction to Subatom Infer
Production-grade runtime data validation, dual sync/async parsing, prototype defense, and static type inference engine for modern JavaScript and TypeScript.
What is Subatom Infer?
When data enters your application—whether from HTTP request bodies, query strings, headers, environment variables, WebSocket events, database results, or file uploads—TypeScript types are erased. Subatom Infer solves this runtime trust boundary problem by verifying shapes at runtime and automatically inferring exact static types with zero overhead.
import { infer, type Infer } from "subatom-infer";
const User = infer.object({
id: infer.string().uuid(),
username: infer.string().min(3).max(20).toLowerCase().trim(),
email: infer.string().email(),
role: infer.enum(["admin", "member"]).default("member"),
createdAt: infer.coerce.date().default(() => new Date())
});
type User = Infer<typeof User>;Core Mental Model
Subatom Infer follows five core principles: 1. Schemas are immutable objects created via factories like infer.string() or infer.object(). 2. Modifiers return a new Schema instance without mutating existing instances. 3. Parsing evaluates a single shared ParseContext across nested paths. 4. Safe parsing guarantees your process will never throw an uncaught exception. 5. Inferred types (Infer<S>, Output<S>, Input<S>) mirror the transformation lifecycle exactly.
Dual Engine (Sync & Async)
Subatom Infer implements an ultra-optimized synchronous engine by default, achieving millions of validations per second. When asynchronous transforms, database checks, or promises are supplied, the schema branches into an asynchronous engine via .parseAsync() or .safeParseAsync() (aliased as .spa()).
// Synchronous execution
const res1 = User.safeParse(input);
// Asynchronous execution
const res2 = await User.safeParseAsync(input);Built-In Prototype Pollution Defense
Unlike naive object mappers, Subatom Infer enforces defensive object creation across infer.object(), .passthrough(), and .merge(). Keys such as __proto__ and constructor are stripped to protect against malicious prototype injection.
