Quick Start Guide
Learn how to define schemas, validate data, and handle errors in under 3 minutes.
1. Define a Schema
Create a schema combining string identifiers, format constraints, numeric ranges, and defaults:
Schema Definition
import { infer } from "subatom-infer";
const ProductSchema = infer.object({
sku: infer.string().regex(/^[A-Z0-9]{8}$/),
title: infer.string().min(3).trim(),
price: infer.number().positive(),
inStock: infer.boolean().default(true),
tags: infer.array(infer.string()).min(1)
});2. Validate Runtime Payloads
Use safeParse() for clean branching without throw-catch blocks:
Schema Definition
const result = ProductSchema.safeParse({
sku: "PROD9901",
title: " Subatom Core Engine ",
price: 49.99,
tags: ["software", "validation"]
});
if (result.success) {
console.log("Success:", result.data.title); // "Subatom Core Engine"
} else {
console.error("Issues:", result.error.flatten());
}3. Derive TypeScript Types
Use Infer<typeof schema> to automatically generate the TypeScript type:
Schema Definition
import { type Infer } from "subatom-infer";
export type Product = Infer<typeof ProductSchema>;
/*
export type Product = {
sku: string;
title: string;
price: number;
inStock: boolean;
tags: string[];
}
*/