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
text/typescript
// ====================================================================
// 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);
}