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