UNPKG

fortify-schema

Version:

A modern TypeScript validation library designed around familiar interface syntax and powerful conditional validation. Experience schema validation that feels natural to TypeScript developers while unlocking advanced runtime validation capabilities.

71 lines (62 loc) โ€ข 2.74 kB
// ==================================================================== // TEST ORIGINAL FAILING CASE // ==================================================================== import { Interface } from "../../schema/mode/interfaces/Interface"; const StartsWithSchema = Interface({ data: "any?", // Prefix checking (FIXED: Use quoted strings for arguments with special characters) isSystemMessage: 'when data.message.$startsWith("SYSTEM:") *? boolean : =no_sysMsg', isErrorCode: 'when data.code.$startsWith("ERR_") *? boolean : =no', // URL checking (FIXED: Use quoted strings for arguments with special characters) isSecureUrl: 'when data.url.$startsWith("https://") *? =secure : =no_secure', }); const data = { message: "SYSTEM: unexpected error in your server, plse retry again", code: "ERR_NOFOUND", url: "https://nehonix.space", }; // Test 1: Wrong values (should fail) console.log("๐Ÿงช Test 1: Wrong values (should fail)"); const result1 = StartsWithSchema.safeParse({ data, isSystemMessage: "no_sysMsg", // WRONG: condition is true, expects boolean isErrorCode: true, // CORRECT: condition is true, expects boolean isSecureUrl: "no_secure", // WRONG: condition is true, expects =secure }); if (result1.success) { console.log("โœ… Unexpected success:", result1.data); } else { console.log("โŒ Expected errors:", result1.errors); } // Test 2: Correct values (should pass) console.log("\n๐Ÿงช Test 2: Correct values (should pass)"); const result2 = StartsWithSchema.safeParse({ data, isSystemMessage: true, // CORRECT: condition is true, expects boolean isErrorCode: false, // CORRECT: condition is true, expects boolean isSecureUrl: "secure", // CORRECT: condition is true, expects =secure }); if (result2.success) { console.log("โœ… Expected success:", result2.data); } else { console.log("โŒ Unexpected errors:", result2.errors); } // Test 3: With non-matching data (should change behavior) console.log("\n๐Ÿงช Test 3: With non-matching data"); const nonMatchingData = { message: "INFO: This is an info message", // Doesn't start with "SYSTEM:" code: "SUCCESS_OK", // Doesn't start with "ERR_" url: "http://example.com", // Doesn't start with "https://" }; const result3 = StartsWithSchema.safeParse({ data: nonMatchingData, isSystemMessage: "no_sysMsg", // CORRECT: condition is false, expects =no_sysMsg isErrorCode: "no", // CORRECT: condition is false, expects =no isSecureUrl: "no_secure", // CORRECT: condition is false, expects =no_secure }); if (result3.success) { console.log("โœ… Expected success:", result3.data); } else { console.log("โŒ Unexpected errors:", result3.errors); }