UNPKG

fortify-schema

Version:

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

122 lines (100 loc) • 4.53 kB
import { Interface } from "../schema/mode/interfaces/Interface"; import { When } from "../schema/extensions/components/ConditionalValidation"; console.log("šŸ”„ NEW CLEAR CONDITIONAL SYNTAX TEST"); console.log("=====================================\n"); console.log("✨ Comparing syntax clarity:"); console.log("āŒ Old confusing: when:role=admin:string[]:number"); console.log("āœ… New clear: when(role=admin) then(string[]) else(number)"); console.log(""); // šŸ”„ NEW CLEAR SYNTAX EXAMPLES console.log("1ļøāƒ£ NEW CLEAR SYNTAX (Recommended!)"); console.log("==================================="); const NewClearSchema = Interface({ // Basic fields role: "admin|user|guest", accountType: "free|premium|enterprise", age: "int(13,120)", // šŸ”„ NEW CLEAR SYNTAX - Much easier to read! permissions: "when(role=admin) then(string[]) else(string[]?)", maxProjects: "when(accountType=free) then(int(1,3)) else(int(1,))", paymentMethod: "when(accountType!=free) then(string) else(string?)", billingAddress: "when(paymentMethod.exists) then(string) else(string?)", accessLevel: "when(age>=18) then(string) else(string?)", // Basic fields id: "positive", email: "email", name: "string(2,50)" }); console.log("āœ… New clear syntax schema created successfully!"); // Test the new clear syntax const newResult = NewClearSchema.safeParse({ role: "admin", accountType: "premium", age: 25, permissions: ["read", "write", "delete"], maxProjects: 50, paymentMethod: "credit_card", billingAddress: "123 Main St", accessLevel: "full", id: 1, email: "admin@example.com", name: "Admin User" }); console.log("āœ… New clear syntax validation:", newResult.success); if (newResult.success && newResult.data) { console.log(" Role:", newResult.data.role); console.log(" Permissions:", newResult.data.permissions); console.log(" Access Level:", newResult.data.accessLevel); } else { console.log(" Errors:", newResult.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 for backward compatibility 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); if (legacyResult.success && legacyResult.data) { console.log(" Backward compatibility maintained!"); } else { console.log(" Errors:", legacyResult.errors); } // šŸŽÆ SYNTAX COMPARISON console.log("\n3ļøāƒ£ SYNTAX CLARITY COMPARISON"); console.log("============================="); console.log("šŸ”„ NEW CLEAR SYNTAX (Recommended):"); console.log(' permissions: "when(role=admin) then(string[]) else(string[]?)"'); console.log(' maxProjects: "when(accountType=free) then(int(1,3)) else(int(1,))"'); console.log(' paymentMethod: "when(accountType!=free) then(string) else(string?)"'); console.log(' accessLevel: "when(age>=18) then(string) else(string?)"'); console.log("\nšŸ”„ LEGACY SYNTAX (Still supported):"); 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("\nšŸ“¦ IMPORT-BASED SYNTAX (Most powerful):"); console.log(" permissions: When.field('role').is('admin').then('string[]').else('string[]?')"); // šŸŽ‰ SUMMARY console.log("\nšŸŽ‰ SYNTAX IMPROVEMENT SUMMARY"); console.log("============================="); console.log("āœ… NEW: Crystal clear syntax with parentheses"); console.log("āœ… LEGACY: Backward compatibility maintained"); console.log("āœ… IMPORT: Full power for complex scenarios"); console.log("āœ… CHOICE: Pick the syntax that fits your needs"); console.log("\nšŸš€ Revolutionary improvement: Conditional validation is now crystal clear!"); console.log(" when(condition) then(schema) else(schema) - Easy to read and understand!"); console.log(" No more confusion about where conditions start and end!");