fortify-schema
Version:
TypeScript interface-like schema validation system that's easier to use than Zod
113 lines (91 loc) ⢠3.7 kB
text/typescript
import { Interface, Make } from "../schema/mode/interfaces/Interface";
console.log("š MAKE.FROMINTERFACE TEST");
console.log("==========================\n");
// Define TypeScript types
type User = {
id: number;
name: string;
email: string;
tags: string[];
metadata: Record<string, any>;
isActive: boolean;
};
type Product = {
id: string;
name: string;
price: number;
categories: string[];
attributes: Record<string, string>;
};
console.log("1. Testing Make.fromInterface<T>() - Automatic schema generation:");
// This should automatically generate the schema from the TypeScript type
const UserSchemaFromInterface = Interface(Make.fromInterface<User>());
console.log("ā
User schema from interface created successfully!");
// Test data
const userData = {
id: 1,
name: "John Doe",
email: "john@example.com",
tags: ["developer", "typescript"],
metadata: { source: "registration", level: "premium" },
isActive: true
};
const userResult = UserSchemaFromInterface.safeParse(userData);
console.log("ā
User validation result:", userResult.success);
if (userResult.success && userResult.data) {
console.log(" User ID:", userResult.data.id);
console.log(" User name:", userResult.data.name);
console.log(" User metadata:", userResult.data.metadata);
// TypeScript should infer exact types
userResult.data.id; // number
userResult.data.name; // string
userResult.data.email; // string
userResult.data.tags; // string[]
userResult.data.metadata; // Record<string, any>
userResult.data.isActive; // boolean
}
console.log("\n2. Testing with Product type:");
const ProductSchemaFromInterface = Interface(Make.fromInterface<Product>());
const productData = {
id: "prod-123",
name: "Laptop",
price: 999.99,
categories: ["electronics", "computers"],
attributes: { brand: "TechCorp", model: "Pro-X1" }
};
const productResult = ProductSchemaFromInterface.safeParse(productData);
console.log("ā
Product validation result:", productResult.success);
if (productResult.success && productResult.data) {
console.log(" Product ID:", productResult.data.id);
console.log(" Product price:", productResult.data.price);
console.log(" Product attributes:", productResult.data.attributes);
// TypeScript should infer exact types
productResult.data.id; // string
productResult.data.name; // string
productResult.data.price; // number
productResult.data.categories; // string[]
productResult.data.attributes; // Record<string, string>
}
console.log("\n3. Comparison with manual schema definition:");
// Manual schema definition (the old way)
const ManualUserSchema = Interface({
id: "number",
name: "string",
email: "string",
tags: "string[]",
metadata: "record<string,any>",
isActive: "boolean"
});
const manualResult = ManualUserSchema.safeParse(userData);
console.log("ā
Manual schema validation result:", manualResult.success);
console.log("\nšÆ ANALYSIS:");
console.log("=============");
console.log("ā
Make.fromInterface<T>() automatically generates schemas from TypeScript types!");
console.log("ā
Perfect type inference - no manual type definitions needed!");
console.log("ā
Zero boilerplate - just pass your TypeScript type!");
console.log("š This is revolutionary - automatic schema generation from types!");
console.log("\nš” USAGE PATTERNS:");
console.log("==================");
console.log("ā
Make.fromType<T['property']>() - for individual properties");
console.log("ā
Make.fromInterface<T>() - for entire TypeScript interfaces");
console.log("ā
Both preserve exact TypeScript types and provide runtime validation!");