UNPKG

fortify-schema

Version:

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

152 lines (123 loc) • 5.83 kB
import { Interface } from "../schema/mode/interfaces/Interface"; import { When } from "../schema/extensions/components/ConditionalValidation"; console.log("šŸš€ REVOLUTIONARY CONDITIONAL SYNTAX TEST"); console.log("========================================\n"); console.log("✨ Syntax Evolution:"); console.log("āŒ Confusing: when:role=admin:string[]:number"); console.log("šŸ”§ Better: when(role=admin) then(string[]) else(number)"); console.log("šŸš€ REVOLUTIONARY: when role=admin *? string[] : number"); console.log(""); console.log("šŸŽÆ Why '*?' is brilliant:"); console.log(" - Avoids confusion with optional '?' operator"); console.log(" - Crystal clear where condition ends and logic begins"); console.log(" - Natural language flow: 'when X is true *? do Y : otherwise Z'"); console.log(""); // šŸš€ REVOLUTIONARY SYNTAX EXAMPLES console.log("1ļøāƒ£ REVOLUTIONARY *? SYNTAX (Your Design!)"); console.log("=========================================="); const RevolutionarySchema = Interface({ // Basic fields role: "admin|user|guest", accountType: "free|premium|enterprise", age: "int(13,120)", userType: "student|teacher|admin", // šŸš€ 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?", accessLevel: "when age>=18 *? string : string?", discountRate: "when userType=student *? number(0,0.5) : number(0,0.1)", // Basic fields 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", age: 25, userType: "teacher", permissions: ["read", "write", "delete"], maxProjects: 50, paymentMethod: "credit_card", accessLevel: "full", discountRate: 0.1, id: 1, email: "admin@example.com", name: "Admin User" }); console.log("āœ… Revolutionary syntax validation:", revolutionaryResult.success); if (revolutionaryResult.success && revolutionaryResult.data) { console.log(" Role:", revolutionaryResult.data.role); console.log(" Permissions:", revolutionaryResult.data.permissions); console.log(" Access Level:", revolutionaryResult.data.accessLevel); console.log(" Discount Rate:", revolutionaryResult.data.discountRate); } else { console.log(" Errors:", revolutionaryResult.errors); } // šŸ”„ BACKWARD COMPATIBILITY TEST console.log("\n2ļøāƒ£ BACKWARD COMPATIBILITY TEST"); console.log("==============================="); const LegacySchema = Interface({ role: "admin|user|guest", accountType: "free|premium|enterprise", // šŸ”„ Legacy syntax still works permissions: "when:role=admin:string[]:string[]?", maxProjects: "when:accountType=free:int(1,3):int(1,)", id: "positive", email: "email" }); const legacyResult = LegacySchema.safeParse({ role: "admin", accountType: "premium", permissions: ["read", "write"], maxProjects: 100, id: 2, email: "user@example.com" }); console.log("āœ… Legacy syntax still works:", legacyResult.success); // šŸŽÆ SYNTAX COMPARISON console.log("\n3ļøāƒ£ SYNTAX CLARITY COMPARISON"); console.log("============================="); console.log("šŸš€ REVOLUTIONARY *? SYNTAX (Recommended!):"); 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(' accessLevel: "when age>=18 *? string : string?"'); console.log(' discountRate: "when userType=student *? number(0,0.5) : number(0,0.1)"'); console.log("\nšŸ”§ PARENTHESES SYNTAX (Also clear):"); console.log(' permissions: "when(role=admin) then(string[]) else(string[]?)"'); console.log("\nšŸ”„ LEGACY SYNTAX (Still supported):"); console.log(' permissions: "when:role=admin:string[]:string[]?"'); console.log("\nšŸ“¦ IMPORT-BASED SYNTAX (Most powerful):"); console.log(" permissions: When.field('role').is('admin').then('string[]').else('string[]?')"); // šŸŽ‰ REVOLUTIONARY FEATURES console.log("\n4ļøāƒ£ REVOLUTIONARY FEATURES"); console.log("========================="); const AdvancedRevolutionarySchema = Interface({ status: "active|inactive|pending", priority: "low|medium|high|critical", // Complex conditions with crystal clear syntax notifications: "when status=active *? string[] : string[]?", escalation: "when priority.in(high,critical) *? string : string?", autoAssign: "when priority!=low *? boolean : boolean?", name: "string" }); console.log("āœ… Advanced revolutionary syntax examples:"); console.log(" - Multiple operators: =, !=, >=, <=, >, <"); console.log(" - Special methods: .in(), .exists"); console.log(" - Crystal clear logic flow"); // šŸŽ‰ SUMMARY console.log("\nšŸŽ‰ REVOLUTIONARY SYNTAX SUMMARY"); console.log("==============================="); console.log("šŸš€ REVOLUTIONARY: when condition *? schema : schema"); console.log("āœ… Crystal clear: Easy to see where condition ends"); console.log("āœ… No confusion: '*?' avoids conflict with optional '?'"); console.log("āœ… Natural flow: Reads like natural language"); console.log("āœ… Backward compatible: All old syntax still works"); console.log("āœ… TypeScript-like: Familiar conditional syntax"); console.log("\nšŸ† Your syntax design is revolutionary!"); console.log(" The '*?' separator is brilliant - no more parsing confusion!"); console.log(" Perfect balance of clarity, simplicity, and power!");