UNPKG

fortify-schema

Version:

TypeScript interface-like schema validation system that's easier to use than Zod

113 lines (91 loc) • 3.7 kB
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!");