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.

147 lines (134 loc) โ€ข 4.27 kB
// ==================================================================== // TEST ORIGINAL FAILING CASE // ==================================================================== import { Interface } from "../../schema/mode/interfaces/Interface"; const DeepAccessSchema = Interface({ data: "any?", // Multi-level property access hasDeepFeature: "when data.user.profile.settings.advanced.features.$exists() *? boolean : =no_deep_ft", // Array property access hasFirstItem: "when data.items[0].$exists() *? boolean : =no_its_hasnt", // Mixed access patterns hasNestedValue: "when data.config.nested.deep.value.$exists() *? boolean : =isnt_nested", }); const data = { items: { x: 23, }, config: { nested: { deep: { value: "Hi there !", }, }, }, user: { profile: { settings: { advanced: { features: { value: "Hi ! You got me, I'm the value you're searching for ๐Ÿ™„๐Ÿ™‚", }, }, }, }, }, }; // Test 1: Wrong values (should fail) console.log("๐Ÿงช Test 1: Wrong values (should fail)"); const result1 = DeepAccessSchema.safeParse({ data, hasDeepFeature: "no_deep_ft", // WRONG: condition is true, expects boolean hasFirstItem: true, // WRONG: condition is false, expects constant "no_its_hasnt" hasNestedValue: "isnt_nested", // WRONG: condition is true, expects boolean }); 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 = DeepAccessSchema.safeParse({ data, hasDeepFeature: true, // CORRECT: condition is true, expects boolean hasFirstItem: "no_its_hasnt", // CORRECT: condition is false, expects constant hasNestedValue: false, // CORRECT: condition is true, expects boolean }); if (result2.success) { console.log("โœ… Expected success:", result2.data); } else { console.log("โŒ Unexpected errors:", result2.errors); } // Test 3: Test with array data (should change behavior) console.log("\n๐Ÿงช Test 3: With array data"); const dataWithArray = { ...data, items: ["first", "second", "third"], // Now it's an array }; const result3 = DeepAccessSchema.safeParse({ data: dataWithArray, hasDeepFeature: true, // condition is true, expects boolean hasFirstItem: true, // NOW condition should be true (items[0] exists), expects boolean hasNestedValue: false, // condition is true, expects boolean }); if (result3.success) { console.log("โœ… Expected success:", result3.data); } else { console.log("โŒ Unexpected errors:", result3.errors); } /** * output: Failed to parse conditional expression: when data.items[0].$exists() *? boolean : =no_its_hasnt [ { type: "SYNTAX_ERROR", message: "Parse error: Expected string key in bracket notation", position: 16, line: 1, column: 17, suggestion: "Check conditional syntax", context: { nearbyTokens: [ { type: "IDENTIFIER", value: "items", position: 10, line: 1, column: 11, }, { type: "LBRACKET", value: "[", position: 15, line: 1, column: 16, }, { type: "NUMBER", value: "0", position: 16, line: 1, column: 17, }, { type: "RBRACKET", value: "]", position: 17, line: 1, column: 18, }, { type: "DOT", value: ".", position: 18, line: 1, column: 19, } ], expectedTokens: [], }, } ] โŒ Unexpected errors: [ "hasDeepFeature: Expected boolean, got string", "hasFirstItem: Unknown or unsuppor t ed type: when data.items[0].$exists() *? boolean : =no_its_hasnt. Please check the type definition.", "hasNestedValue: Expected boolean, got string" ] */