fortify-schema
Version:
TypeScript interface-like schema validation system that's easier to use than Zod
152 lines (123 loc) ⢠5.83 kB
text/typescript
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!");