|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | +import { z } from 'zod/v3'; |
| 4 | +import { z as z4 } from 'zod/v4'; |
| 5 | +import { zod } from '$lib/adapters/zod.js'; |
| 6 | +import { zod as zod4 } from '$lib/adapters/zod4.js'; |
| 7 | +import { superValidate } from '$lib/superValidate.js'; |
| 8 | + |
| 9 | +describe('Issue 646 - Top-level transform and refine support', () => { |
| 10 | + describe('Zod v3 adapter', () => { |
| 11 | + it('should accept schema with top-level transform (TypeScript compilation test)', async () => { |
| 12 | + const schema = z |
| 13 | + .object({ |
| 14 | + name: z.string() |
| 15 | + }) |
| 16 | + .transform((s) => s); |
| 17 | + |
| 18 | + // Should not throw TypeScript error - this is the main fix |
| 19 | + const form = await superValidate(zod(schema)); |
| 20 | + expect(form).toBeDefined(); |
| 21 | + // Note: Top-level transforms change the output type, making introspection difficult |
| 22 | + }); |
| 23 | + |
| 24 | + it('should accept schema with top-level refine', async () => { |
| 25 | + const schema = z |
| 26 | + .object({ |
| 27 | + name: z.string() |
| 28 | + }) |
| 29 | + .refine((data) => data.name.length > 0, { |
| 30 | + message: 'Name is required' |
| 31 | + }); |
| 32 | + |
| 33 | + // Should not throw TypeScript error - this is the main fix |
| 34 | + const validForm = await superValidate({ name: 'test' }, zod(schema)); |
| 35 | + expect(validForm.valid).toBe(true); |
| 36 | + |
| 37 | + // Test with invalid data |
| 38 | + const invalidForm = await superValidate({ name: '' }, zod(schema)); |
| 39 | + expect(invalidForm.valid).toBe(false); |
| 40 | + // Root-level refine errors appear in _errors |
| 41 | + expect(invalidForm.errors._errors).toEqual(['Name is required']); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should work with field-level transform', async () => { |
| 45 | + const schema = z.object({ |
| 46 | + name: z.string().transform((s) => s.toUpperCase()) |
| 47 | + }); |
| 48 | + |
| 49 | + const form = await superValidate({ name: 'test' }, zod(schema)); |
| 50 | + expect(form.valid).toBe(true); |
| 51 | + expect(form.data.name).toBe('TEST'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should work with superRefine at top level', async () => { |
| 55 | + const schema = z |
| 56 | + .object({ |
| 57 | + password: z.string(), |
| 58 | + confirmPassword: z.string() |
| 59 | + }) |
| 60 | + .superRefine((data, ctx) => { |
| 61 | + if (data.password !== data.confirmPassword) { |
| 62 | + ctx.addIssue({ |
| 63 | + code: z.ZodIssueCode.custom, |
| 64 | + message: 'Passwords do not match', |
| 65 | + path: ['confirmPassword'] |
| 66 | + }); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + const validForm = await superValidate( |
| 71 | + { password: 'test123', confirmPassword: 'test123' }, |
| 72 | + zod(schema) |
| 73 | + ); |
| 74 | + expect(validForm.valid).toBe(true); |
| 75 | + |
| 76 | + const invalidForm = await superValidate( |
| 77 | + { password: 'test123', confirmPassword: 'different' }, |
| 78 | + zod(schema) |
| 79 | + ); |
| 80 | + expect(invalidForm.valid).toBe(false); |
| 81 | + expect(invalidForm.errors.confirmPassword).toEqual(['Passwords do not match']); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('Zod v4 adapter', () => { |
| 86 | + it('should accept schema with top-level transform (issue #646 - TypeScript fix)', async () => { |
| 87 | + const brokenSchema = z4 |
| 88 | + .object({ |
| 89 | + name: z4.string() |
| 90 | + }) |
| 91 | + .transform((s) => s); |
| 92 | + |
| 93 | + // This should NOT throw TypeScript error anymore - THIS IS THE FIX FOR #646 |
| 94 | + // Before the fix, this would fail with: |
| 95 | + // "Argument of type ZodPipe<...> is not assignable to parameter of type ZodValidationSchema" |
| 96 | + |
| 97 | + // Note: Top-level transforms prevent schema introspection, so this will throw at runtime |
| 98 | + // The important fix is that it compiles without TypeScript errors |
| 99 | + try { |
| 100 | + await superValidate({ name: 'test' }, zod4(brokenSchema)); |
| 101 | + // If we get here, great! The schema introspection worked |
| 102 | + } catch (error) { |
| 103 | + // Expected: "No shape could be created for schema" |
| 104 | + // This is a runtime limitation, not a TypeScript error |
| 105 | + expect((error as Error).message).toContain('No shape could be created'); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + it('should accept schema with top-level refine', async () => { |
| 110 | + const schema = z4 |
| 111 | + .object({ |
| 112 | + name: z4.string() |
| 113 | + }) |
| 114 | + .refine((data) => data.name.length > 0, { |
| 115 | + message: 'Name is required' |
| 116 | + }); |
| 117 | + |
| 118 | + // Should not throw TypeScript error - this is the main fix |
| 119 | + const validForm = await superValidate({ name: 'test' }, zod4(schema)); |
| 120 | + expect(validForm.valid).toBe(true); |
| 121 | + |
| 122 | + // Test with invalid data |
| 123 | + const invalidForm = await superValidate({ name: '' }, zod4(schema)); |
| 124 | + expect(invalidForm.valid).toBe(false); |
| 125 | + // Root-level refine errors appear in _errors |
| 126 | + expect(invalidForm.errors._errors).toEqual(['Name is required']); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should work with field-level transform (the working example from issue)', async () => { |
| 130 | + const workingSchema = z4.object({ |
| 131 | + name: z4.string().transform((s) => s.toUpperCase()) |
| 132 | + }); |
| 133 | + |
| 134 | + const form = await superValidate({ name: 'test' }, zod4(workingSchema)); |
| 135 | + expect(form.valid).toBe(true); |
| 136 | + expect(form.data.name).toBe('TEST'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should work with superRefine at top level', async () => { |
| 140 | + const schema = z4 |
| 141 | + .object({ |
| 142 | + password: z4.string(), |
| 143 | + confirmPassword: z4.string() |
| 144 | + }) |
| 145 | + .superRefine((data, ctx) => { |
| 146 | + if (data.password !== data.confirmPassword) { |
| 147 | + ctx.addIssue({ |
| 148 | + code: 'custom' as any, |
| 149 | + message: 'Passwords do not match', |
| 150 | + path: ['confirmPassword'] |
| 151 | + }); |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + const validForm = await superValidate( |
| 156 | + { password: 'test123', confirmPassword: 'test123' }, |
| 157 | + zod4(schema) |
| 158 | + ); |
| 159 | + expect(validForm.valid).toBe(true); |
| 160 | + |
| 161 | + const invalidForm = await superValidate( |
| 162 | + { password: 'test123', confirmPassword: 'different' }, |
| 163 | + zod4(schema) |
| 164 | + ); |
| 165 | + expect(invalidForm.valid).toBe(false); |
| 166 | + expect(invalidForm.errors.confirmPassword).toEqual(['Passwords do not match']); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('Comparison between Zod v3 and v4', () => { |
| 171 | + it('should handle top-level refines identically', async () => { |
| 172 | + const schema3 = z.object({ value: z.number() }).refine((data) => data.value > 0, { |
| 173 | + message: 'Value must be positive' |
| 174 | + }); |
| 175 | + |
| 176 | + const schema4 = z4.object({ value: z4.number() }).refine((data) => data.value > 0, { |
| 177 | + message: 'Value must be positive' |
| 178 | + }); |
| 179 | + |
| 180 | + const form3Valid = await superValidate({ value: 5 }, zod(schema3)); |
| 181 | + const form4Valid = await superValidate({ value: 5 }, zod4(schema4)); |
| 182 | + |
| 183 | + expect(form3Valid.valid).toBe(true); |
| 184 | + expect(form4Valid.valid).toBe(true); |
| 185 | + |
| 186 | + const form3Invalid = await superValidate({ value: -5 }, zod(schema3)); |
| 187 | + const form4Invalid = await superValidate({ value: -5 }, zod4(schema4)); |
| 188 | + |
| 189 | + expect(form3Invalid.valid).toBe(false); |
| 190 | + expect(form4Invalid.valid).toBe(false); |
| 191 | + expect(form3Invalid.errors._errors).toEqual(form4Invalid.errors._errors); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments