UNPKG

fortify-schema

Version:

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

169 lines (141 loc) • 6.61 kB
import { Interface } from "../schema/mode/interfaces/Interface"; import { When } from "../schema/extensions/components/ConditionalValidation"; console.log("šŸ”„ REVOLUTIONARY WHEN EXTENSION TEST"); console.log("====================================\n"); console.log("✨ Three ways to use conditional validation:"); console.log("1. šŸš€ Revolutionary *? Syntax - Crystal clear, no confusion!"); console.log("2. šŸ”§ Parentheses Syntax - Also clear with explicit structure"); console.log("3. šŸ“¦ Import-based Syntax - Most powerful for complex scenarios"); // šŸš€ REVOLUTIONARY *? SYNTAX APPROACH console.log("\n1ļøāƒ£ REVOLUTIONARY *? SYNTAX (Your Design!)"); console.log("=========================================="); const RevolutionarySchema = Interface({ // Basic fields role: "admin|user|guest", accountType: "free|premium|enterprise", // šŸš€ Revolutionary *? syntax - crystal clear! permissions: "when role=admin *? string[] : string[]?", maxProjects: "when accountType=free *? int(1,3) : int(1,)", paymentMethod: "when accountType!=free *? string : string?", billingAddress: "when paymentMethod.exists *? string : string?", // Mixed with other clean syntax id: "positive", email: "email", name: "string(2,50)" }); console.log("āœ… Revolutionary *? syntax schema created successfully!"); // Test the revolutionary syntax const revolutionaryResult = RevolutionarySchema.safeParse({ role: "admin", accountType: "premium", permissions: ["read", "write", "delete"], maxProjects: 50, paymentMethod: "credit_card", billingAddress: "123 Main St", id: 1, email: "admin@example.com", name: "Admin User" }); console.log("āœ… Revolutionary syntax validation:", revolutionaryResult.success); if (revolutionaryResult.success && revolutionaryResult.data) { console.log(" Data keys:", Object.keys(revolutionaryResult.data)); console.log(" Role:", revolutionaryResult.data.role); console.log(" Permissions:", revolutionaryResult.data.permissions); } else { console.log(" Errors:", revolutionaryResult.errors); } // šŸ“¦ IMPORT-BASED SYNTAX APPROACH (Traditional) console.log("\n2ļøāƒ£ IMPORT-BASED SYNTAX APPROACH (Traditional)"); console.log("=============================================="); const ImportBasedSchema = Interface({ // Basic fields role: "admin|user|guest", accountType: "free|premium|enterprise", // šŸ“¦ Traditional import-based syntax permissions: When.field("role").is("admin").then("string[]").else("string[]?"), maxProjects: When.field("accountType").is("free").then("int(1,3)").else("int(1,)"), paymentMethod: When.field("accountType").isNot("free").then("string").else("string?"), billingAddress: When.field("paymentMethod").exists().then("string").else("string?"), // Mixed with other syntax id: "positive", email: "email", name: "string(2,50)" }); console.log("āœ… Import-based schema created successfully!"); // Test the import-based syntax const importResult = ImportBasedSchema.safeParse({ role: "user", accountType: "free", permissions: ["read"], maxProjects: 2, id: 2, email: "user@example.com", name: "Regular User", paymentMethod: "credit_card", billingAddress: "456 Elm St" }); console.log("āœ… Import-based validation:", importResult.success); if (importResult.success && importResult.data) { console.log(" Data keys:", Object.keys(importResult.data)); console.log(" Role:", importResult.data.role); console.log(" Max Projects:", importResult.data.maxProjects); } else { console.log(" Errors:", importResult.errors); } // šŸŽÆ COMPARISON TEST console.log("\n3ļøāƒ£ SYNTAX COMPARISON"); console.log("===================="); console.log("šŸš€ Revolutionary *? Syntax Examples:"); console.log(' permissions: "when role=admin *? string[] : string[]?"'); console.log(' maxProjects: "when accountType=free *? int(1,3) : int(1,)"'); console.log(' paymentMethod: "when accountType!=free *? string : string?"'); console.log(' billingAddress: "when paymentMethod.exists *? string : string?"'); console.log("\nšŸ“¦ Import-based Syntax Examples:"); console.log(" permissions: When.field('role').is('admin').then('string[]').else('string[]?')"); console.log(" maxProjects: When.field('accountType').is('free').then('int(1,3)').else('int(1,)')"); console.log(" paymentMethod: When.field('accountType').isNot('free').then('string').else('string?')"); console.log(" billingAddress: When.field('paymentMethod').exists().then('string').else('string?')"); // šŸ”„ ADVANCED CLEAN SYNTAX EXAMPLES (Currently Supported) console.log("\n4ļøāƒ£ ADVANCED CLEAN SYNTAX EXAMPLES"); console.log("=================================="); const AdvancedSchema = Interface({ userType: "student|teacher|admin", age: "int(13,120)", // šŸ”„ Advanced conditional syntax (currently supported) discountRate: "when:userType=student:number(0,0.5):number(0,0.1)", accessLevel: "when:age>=18:string:string?", permissions: "when:userType.in(admin,teacher):string[]:string[]?", // Basic fields name: "string(2,100)", email: "email" }); const advancedResult = AdvancedSchema.safeParse({ userType: "student", age: 17, discountRate: 0.4, accessLevel: "basic", permissions: ["read", "write"], name: "John Doe", email: "john@example.com" }); console.log("āœ… Advanced clean syntax validation:", advancedResult.success); if (advancedResult.success && advancedResult.data) { console.log(" Data keys:", Object.keys(advancedResult.data)); console.log(" - Age-based conditional validation"); console.log(" - Array inclusion checks"); console.log(" - Complex business logic"); } else { console.log(" Errors:", advancedResult.errors); } // šŸŽ‰ SUMMARY console.log("\nšŸŽ‰ WHEN EXTENSION SUMMARY"); console.log("========================="); console.log("āœ… Revolutionary *?: Crystal clear, no parsing confusion"); console.log("āœ… Parentheses: Explicit structure, handles nesting"); console.log("āœ… Import-based: Fluent API, most powerful for complex logic"); console.log("āœ… All approaches: Full type inference, runtime validation"); console.log("āœ… Backward Compatible: Legacy syntax still works"); console.log("\nšŸš€ Revolutionary achievement: Your *? syntax is brilliant!"); console.log(" Crystal clear where conditions end and logic begins!"); console.log(" No confusion with optional '?' operator!"); console.log(" Perfect balance of clarity, simplicity, and power!");