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.

80 lines (69 loc) โ€ข 2.85 kB
// ==================================================================== // TEST ORIGINAL FAILING CASE // ==================================================================== import { Interface } from "../../schema/mode/interfaces/Interface"; const CombinedMethodsSchema = Interface({ user: "any?", config: "any?", // Logical AND isValidUser: "when user.email.$exists() && user.verified.$exists() *? boolean : =no_valid", // Logical OR hasContact: "when user.email.$exists() || user.phone.$exists() *? boolean : =no_contact", // Complex combinations canAccessFeature: "when user.role.$in(admin,premium) && config.features.$contains(advanced) *? boolean : =no_feature", // Nested conditions accessLevel: "when user.role=admin *? when config.superAdmin.$exists() *? =super : =admin : =user", }); const config = { features: "feature.....advanced", superAdmin: "Yeah I'm your superadmin, welcome ๐Ÿ˜Ž", }; const user = { email: "mymail@domain.com", // email exist in user // verified: true, // not exist role: "admin", }; // Test 1: Wrong values (should fail) console.log("๐Ÿงช Test 1: Wrong values (should fail)"); const result1 = CombinedMethodsSchema.safeParse({ config, user, isValidUser: true, // WRONG: condition is false, expects =no_valid hasContact: "no_contact", // WRONG: condition is true, expects boolean canAccessFeature: "no_feature", // WRONG: condition is true, expects boolean accessLevel: "no_super", // WRONG: should be =super (nested condition) }); 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 = CombinedMethodsSchema.safeParse({ config, user, isValidUser: "no_valid", // CORRECT: condition is false, expects =no_valid hasContact: true, // CORRECT: condition is true, expects boolean canAccessFeature: false, // CORRECT: condition is true, expects boolean accessLevel: "super", // CORRECT: nested condition should result in =super }); if (result2.success) { console.log("โœ… Expected success:", result2.data); } else { console.log("โŒ Unexpected errors:", result2.errors); } /** * bug: * accessLevel: "no_super", should throw error, it has throwed error but not what we're expecting. * Here's throwed errors: * * โŒ Unexpected errors: [ "isValidUser: Expected constant value 'no_valid', got 'true'", "hasContact: Expected boolean, got string", "canAccessFeature: Expected boolean, got string", "accessLevel: Evaluation error: Error: Error: Error: Invalid property path: user.role. Cannot access property \"role\" on non-object at path: user" ] * */