UNPKG

fortify-schema

Version:

TypeScript interface-like schema validation system that's easier to use than Zod

161 lines (134 loc) • 4.59 kB
import { Interface, SchemaHelpers as SH, SchemaUtils } from "../schema/mode/interfaces/Interface"; console.log("šŸ› ļø Testing SchemaUtils - Comprehensive Test Suite\n"); // Base schemas for testing const UserSchema = Interface({ id: "number", name: "string", email: "email", password: "string" }); const ProfileSchema = Interface({ bio: "string?", avatar: "url?", age: "number?" }); const SettingsSchema = Interface({ theme: SH.union("light", "dark"), notifications: "boolean?", language: "string?" }); // Test 1: Merge schemas console.log("1ļøāƒ£ Testing SchemaUtils.merge()"); const MergedSchema = SchemaUtils.merge(UserSchema, ProfileSchema); const mergeTest = MergedSchema.safeParse({ id: 1, name: "John Doe", email: "john@example.com", password: "secret123", bio: "I love TypeScript!", avatar: "https://example.com/avatar.jpg", age: 25 }); console.log("āœ… Merge test:", mergeTest.success); if (mergeTest.success && mergeTest.data) { console.log(" Merged data keys:", Object.keys(mergeTest.data)); console.log(" Bio:", mergeTest.data.bio); console.log(" Age:", mergeTest.data.age); } // Test 2: Pick specific fields console.log("\n2ļøāƒ£ Testing SchemaUtils.pick()"); const PublicUserSchema = SchemaUtils.pick(UserSchema, ["id", "name", "email"]); const pickTest = PublicUserSchema.safeParse({ id: 1, name: "John Doe", email: "john@example.com" }); console.log("āœ… Pick test:", pickTest.success); if (pickTest.success && pickTest.data) { console.log(" Picked data keys:", Object.keys(pickTest.data)); } // Test 3: Omit sensitive fields console.log("\n3ļøāƒ£ Testing SchemaUtils.omit()"); const SafeUserSchema = SchemaUtils.omit(UserSchema, ["password"]); const omitTest = SafeUserSchema.safeParse({ id: 1, name: "John Doe", email: "john@example.com" }); console.log("āœ… Omit test:", omitTest.success); if (omitTest.success && omitTest.data) { console.log(" Omitted data keys:", Object.keys(omitTest.data)); } // Test 4: Make all fields optional console.log("\n4ļøāƒ£ Testing SchemaUtils.partial()"); const PartialUserSchema = SchemaUtils.partial(UserSchema); const partialTest = PartialUserSchema.safeParse({ id: 1, name: "John Doe" // email and password are now optional }); console.log("āœ… Partial test:", partialTest.success); if (partialTest.success && partialTest.data) { console.log(" Partial data keys:", Object.keys(partialTest.data)); } // Test 5: Make all fields required console.log("\n5ļøāƒ£ Testing SchemaUtils.required()"); const RequiredProfileSchema = SchemaUtils.required(ProfileSchema); const requiredTest = RequiredProfileSchema.safeParse({ bio: "I love coding!", avatar: "https://example.com/avatar.jpg", age: 30 }); console.log("āœ… Required test:", requiredTest.success); if (requiredTest.success && requiredTest.data) { console.log(" Required data keys:", Object.keys(requiredTest.data)); } // Test 6: Extend schema with new fields console.log("\n6ļøāƒ£ Testing SchemaUtils.extend()"); const ExtendedUserSchema = SchemaUtils.extend(UserSchema, { createdAt: "date", isActive: "boolean", role: SH.const("user") }); const extendTest = ExtendedUserSchema.safeParse({ id: 1, name: "John Doe", email: "john@example.com", password: "secret123", createdAt: new Date(), isActive: true, role: "user" }); console.log("āœ… Extend test:", extendTest.success); if (extendTest.success && extendTest.data) { console.log(" Extended data keys:", Object.keys(extendTest.data)); console.log(" Role:", extendTest.data.role); console.log(" IsActive:", extendTest.data.isActive); } // Test 7: Complex chaining console.log("\n7ļøāƒ£ Testing Complex Chaining"); const ComplexSchema = SchemaUtils.extend( SchemaUtils.omit( SchemaUtils.merge(UserSchema, ProfileSchema), ["password"] ), { lastLogin: "date?", preferences: SH.union("minimal", "detailed") } ); const complexTest = ComplexSchema.safeParse({ id: 1, name: "John Doe", email: "john@example.com", bio: "Full-stack developer", age: 28, lastLogin: new Date(), preferences: "detailed" }); console.log("āœ… Complex chaining test:", complexTest.success); if (complexTest.success && complexTest.data) { console.log(" Complex data keys:", Object.keys(complexTest.data)); console.log(" Preferences:", complexTest.data.preferences); } console.log("\nšŸŽ‰ All SchemaUtils tests completed!");