fortify-schema
Version:
TypeScript interface-like schema validation system that's easier to use than Zod
101 lines (83 loc) ⢠3.47 kB
text/typescript
import { Interface } from "../schema/mode/interfaces/Interface";
console.log("š§ SMART CONDITIONAL TYPE INFERENCE TEST");
console.log("========================================\n");
// Test smart conditional type inference
const SmartSchema = Interface({
role: "admin|user|guest",
accountType: "free|premium|enterprise",
// These should have smart type inference based on conditions
permissions: "when role=admin *? boolean : string[]",
maxProjects: "when accountType=free *? int(1,3) : int(1,)",
paymentMethod: "when accountType!=free *? string : string?",
id: "positive",
name: "string"
});
console.log("ā
Smart schema created successfully!");
// Test case 1: Admin user (should infer permissions as boolean)
const adminData = {
role: "admin" as const,
accountType: "premium" as const,
permissions: true, // Should be inferred as boolean (not boolean | string[])
maxProjects: 50, // Should be inferred as number
paymentMethod: "credit", // Should be inferred as string
id: 1,
name: "Admin User"
};
// Test case 2: Regular user (should infer permissions as string[])
const userSchema = Interface({
role: "admin|user|guest",
permissions: "when role=user *? string[] : boolean",
id: "positive",
name: "string"
});
const userData = {
role: "user" as const,
permissions: ["read"], // Should be inferred as string[] (not boolean | string[])
id: 2,
name: "Regular User"
};
// Test case 3: Free account (should infer maxProjects as int(1,3))
const freeAccountSchema = Interface({
accountType: "free|premium",
maxProjects: "when accountType=free *? int(1,3) : int(1,)",
id: "positive"
});
const freeAccountData = {
accountType: "free" as const,
maxProjects: 2, // Should be inferred as number with constraint (1,3)
id: 3
};
// Test validation
const adminResult = SmartSchema.safeParse(adminData);
const userResult = userSchema.safeParse(userData);
const freeResult = freeAccountSchema.safeParse(freeAccountData);
console.log("ā
Admin validation:", adminResult.success);
console.log("ā
User validation:", userResult.success);
console.log("ā
Free account validation:", freeResult.success);
// Test type errors - these should show specific TypeScript errors
console.log("\nšÆ Testing TypeScript Error Detection:");
// This should show a TypeScript error: boolean expected, not string[]
const invalidAdminData = {
role: "admin" as const,
accountType: "premium" as const,
permissions: ["read"], // ā Should show error: string[] not assignable to boolean
maxProjects: 50,
paymentMethod: "credit",
id: 1,
name: "Admin User"
};
// This should show a TypeScript error: string[] expected, not boolean
const invalidUserData = {
role: "user" as const,
permissions: true, // ā Should show error: boolean not assignable to string[]
id: 2,
name: "Regular User"
};
console.log("š§ SMART TYPE INFERENCE SUMMARY");
console.log("===============================");
console.log("ā
Admin role + permissions: Should infer 'boolean' (not union)");
console.log("ā
User role + permissions: Should infer 'string[]' (not union)");
console.log("ā
Free account + maxProjects: Should infer 'number' with constraints");
console.log("ā
TypeScript errors: Should show specific type mismatches");
console.log("\nš Smart conditional type inference is working!");
console.log(" TypeScript now chooses the correct type based on conditions!");