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.

131 lines (110 loc) 4.53 kB
import { Interface } from "../../schema/mode/interfaces/Interface"; const ComplexUnionSchema = Interface({ // Union with constraints identifier: "string(3,)|number(1,)", // String 3+ chars OR number 1+ // Union with formats contact: "email|phone", // Email OR phone number // Union with arrays data: "string[]|number[]", // Array of strings OR numbers // Union with regex code: "string(/^[A-Z]{3}$/)|string(/^\\d{6}$/)", // 3 letters OR 6 digits // Nested unions in objects config: { mode: "development|staging|production", debug: "boolean|string", port: "number|string", }, }); const config = { mode: "development", debug: true, port: true, }; // Test 1: Should fail const result1 = ComplexUnionSchema.safeParse({ identifier: "hi", //should fail - too short for string(3,) contact: "email", //should pass - literal value data: ["test"], //should pass - string[] code: "hi", //should fail - doesn't match regex patterns config, //should fail - port is boolean instead of number|string }); console.log("Test 1 (should have some failures):"); if (result1.success) { console.log("✅ Unexpected success:", result1.data); } else { console.log("❌ Expected errors:", result1.errors); } // Test 2: Should pass const result2 = ComplexUnionSchema.safeParse({ identifier: "hello", //should pass - string(3,) satisfied contact: "phone", //should pass - literal value data: [1, 2, 3], //should pass - number[] code: "ABC", //should pass - matches /^[A-Z]{3}$/ config: { mode: "production", //should pass debug: "verbose", //should pass - string in boolean|string union port: 3000, //should pass - number in number|string union }, }); console.log("\nTest 2 (should pass):"); if (result2.success) { console.log("✅ Expected success:", result2.data); } else { console.log("❌ Unexpected errors:", result2.errors); } /** * bugs * output: * PS F:\Projects\NEHONIX\fortifyjs\src\core\schema> bun src\core\private\gl.ts ❌ Unexpected errors: [ "identifier: Union validation failed: Expected one of: number(1,), string(3,), got hi", "data: Required field is missing", "code: Union validation failed: Expected one of: string(/^[A-Z]{3}$/), string(/^\\d{6}$/), got hi", "config: debug: Union validation failed: Expected one of: boolean, string, got true", "config: port: Union validation failed: Expected one of: number, string, got true" ] PS F:\Projects\NEHONIX\fortifyjs\src\core\schema> */ /** * Since in a union syntax, we can have value or type, so we need to adapt the logic. * As allowed types, we'll only accept types like: * string; number ; null and boolean * other format apart these types will be considered as normal values but * to overide, let implement a syntax (simple; easy to undestand and pro) for that (suggest one and propose). * * @example * * 1) string|number => should be interpreted as types since its contains reserved keywords for union * 1) string[]()|number() => should be interpreted as types since its contains reserved keywords for union and allow all contrainst, I mean methods like min,max and other * 2) boolean|nomal_text => boolean should be interpreted as type but other should be done as normal format (actual format for example) * 3) []|email => sould be interpreted as normal val like actual output do, no change, no considered as types because itsn't in reserved words for unions so we'll have: email or [] */ /** * @example other sample * const OptionalVsNullableSchema = Interface({ // Optional - can be undefined, not present in object optionalField: "string?", // Nullable - must be present, can be null nullableField: "string|null", // Both optional and nullable flexibleField: "string|null?", // Array variations optionalArray: "string[]?", // Array can be undefined nullableArray: "string[]|null", // Array can be null arrayOfOptional: "string?[]", // Array of optional strings }); const profile = { lastName: "Eleazar", }; const result = OptionalVsNullableSchema.safeParse({ nullableField: null, }); if (result.success) { console.log("✅ Expected success:", result.data); } else { console.log("❌ Unexpected errors:", result.errors); } output: ❌ Unexpected errors: [ "nullableField: Field cannot be null", "nullableArray: Required field is missing", "arrayOfOptional: Required field is missing" ] */