UNPKG

fortify-schema

Version:

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

81 lines (62 loc) • 2.67 kB
import { Interface } from "../schema/mode/interfaces/Interface"; console.log("šŸ”§ SIMPLE PARENTHESES SYNTAX TEST"); console.log("=================================\n"); // Test simple parentheses syntax without nested parentheses const SimpleParenthesesSchema = Interface({ role: "admin|user|guest", // Simple parentheses syntax without nested parentheses permissions: "when(role=admin) then(string) else(string?)", id: "positive", name: "string" }); console.log("āœ… Simple parentheses syntax schema created successfully!"); // Test TypeScript type inference const testData = { role: "admin" as const, permissions: "read", // Should be inferred as string id: 1, name: "Admin User" }; const result = SimpleParenthesesSchema.safeParse(testData); console.log("āœ… Simple parentheses test:", result.success); // Test with type error - this should show TypeScript error if type inference works const invalidData = { role: "admin" as const, permissions: 123, // āŒ Should show TypeScript error: number instead of string id: 1, name: "Admin User" }; console.log("šŸŽÆ Testing TypeScript error detection for simple parentheses syntax..."); console.log("If you see a TypeScript error above, type inference is working!"); // Now test with array syntax that has nested parentheses const ArrayParenthesesSchema = Interface({ role: "admin|user|guest", // This has nested parentheses: string[] permissions: "when(role=admin) then(string[]) else(string[]?)", id: "positive", name: "string" }); console.log("āœ… Array parentheses syntax schema created successfully!"); // Test with array data const arrayTestData = { role: "admin" as const, permissions: ["read", "write"], // Should be inferred as string[] id: 1, name: "Admin User" }; const arrayResult = ArrayParenthesesSchema.safeParse(arrayTestData); console.log("āœ… Array parentheses test:", arrayResult.success); // Test with array type error const invalidArrayData = { role: "admin" as const, permissions: ["read", "write", 2], // āŒ Should show TypeScript error: number in string[] id: 1, name: "Admin User" }; console.log("šŸŽÆ Testing TypeScript error detection for array parentheses syntax..."); console.log("If you see a TypeScript error above, array type inference is working!"); console.log("\nšŸ”§ PARENTHESES SYNTAX ANALYSIS"); console.log("=============================="); console.log("āœ… Simple parentheses: when(condition) then(string) else(string?)"); console.log("šŸ”§ Array parentheses: when(condition) then(string[]) else(string[]?)"); console.log("šŸ“ The issue might be with nested parentheses in TypeScript template literals");