|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { z } from 'zod/v4'; |
| 3 | +import { zod, zodToJSONSchema } from '$lib/adapters/zod4.js'; |
| 4 | +import { superValidate } from '$lib/server/index.js'; |
| 5 | + |
| 6 | +describe('issue-617: z.set() and z.map() with default values', () => { |
| 7 | + describe('JSON Schema generation', () => { |
| 8 | + it('should generate correct schema for z.set() with empty default', () => { |
| 9 | + const schema = z.object({ |
| 10 | + set: z.set(z.number()).default(new Set()) |
| 11 | + }); |
| 12 | + |
| 13 | + const jsonSchema = zodToJSONSchema(schema); |
| 14 | + expect(jsonSchema.properties?.set).toMatchObject({ |
| 15 | + type: 'array', |
| 16 | + uniqueItems: true, |
| 17 | + default: [] |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should generate correct schema for z.set() with pre-filled default', () => { |
| 22 | + const schema = z.object({ |
| 23 | + set: z.set(z.number()).default(new Set([1, 2, 3])) |
| 24 | + }); |
| 25 | + |
| 26 | + const jsonSchema = zodToJSONSchema(schema); |
| 27 | + expect(jsonSchema.properties?.set).toMatchObject({ |
| 28 | + type: 'array', |
| 29 | + uniqueItems: true, |
| 30 | + default: [1, 2, 3] |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should generate correct schema for z.map() with empty default', () => { |
| 35 | + const schema = z.object({ |
| 36 | + map: z.map(z.string(), z.number()).default(new Map()) |
| 37 | + }); |
| 38 | + |
| 39 | + const jsonSchema = zodToJSONSchema(schema); |
| 40 | + expect(jsonSchema.properties?.map).toMatchObject({ |
| 41 | + type: 'array', |
| 42 | + default: [] |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should generate correct schema for z.map() with pre-filled default', () => { |
| 47 | + const schema = z.object({ |
| 48 | + map: z.map(z.string(), z.number()).default( |
| 49 | + new Map([ |
| 50 | + ['a', 1], |
| 51 | + ['b', 2] |
| 52 | + ]) |
| 53 | + ) |
| 54 | + }); |
| 55 | + |
| 56 | + const jsonSchema = zodToJSONSchema(schema); |
| 57 | + expect(jsonSchema.properties?.map).toMatchObject({ |
| 58 | + type: 'array', |
| 59 | + default: [ |
| 60 | + ['a', 1], |
| 61 | + ['b', 2] |
| 62 | + ] |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle z.set() with empty default', async () => { |
| 68 | + const schema = z.object({ |
| 69 | + set: z.set(z.number()).default(new Set()) |
| 70 | + }); |
| 71 | + |
| 72 | + const form = await superValidate(zod(schema)); |
| 73 | + expect(form.data.set).toBeInstanceOf(Set); |
| 74 | + expect(form.data.set.size).toBe(0); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle z.set() with pre-filled default values', async () => { |
| 78 | + const schema = z.object({ |
| 79 | + set: z.set(z.number()).default(new Set([1, 2, 3])) |
| 80 | + }); |
| 81 | + |
| 82 | + const jsonSchema = zodToJSONSchema(schema); |
| 83 | + expect(jsonSchema.properties?.set).toMatchObject({ |
| 84 | + type: 'array', |
| 85 | + uniqueItems: true, |
| 86 | + default: [1, 2, 3] |
| 87 | + }); |
| 88 | + |
| 89 | + const form = await superValidate(zod(schema)); |
| 90 | + expect(form.data.set).toBeInstanceOf(Set); |
| 91 | + expect(form.data.set.size).toBe(3); |
| 92 | + expect(form.data.set.has(1)).toBe(true); |
| 93 | + expect(form.data.set.has(2)).toBe(true); |
| 94 | + expect(form.data.set.has(3)).toBe(true); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should handle z.map() with empty default', async () => { |
| 98 | + const schema = z.object({ |
| 99 | + map: z.map(z.string(), z.number()).default(new Map()) |
| 100 | + }); |
| 101 | + |
| 102 | + const jsonSchema = zodToJSONSchema(schema); |
| 103 | + expect(jsonSchema.properties?.map).toMatchObject({ |
| 104 | + type: 'array', |
| 105 | + default: [] |
| 106 | + }); |
| 107 | + |
| 108 | + const form = await superValidate(zod(schema)); |
| 109 | + expect(form.data.map).toBeInstanceOf(Map); |
| 110 | + expect(form.data.map.size).toBe(0); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should handle z.map() with pre-filled default values', async () => { |
| 114 | + const schema = z.object({ |
| 115 | + map: z.map(z.string(), z.number()).default( |
| 116 | + new Map([ |
| 117 | + ['a', 1], |
| 118 | + ['b', 2] |
| 119 | + ]) |
| 120 | + ) |
| 121 | + }); |
| 122 | + |
| 123 | + const jsonSchema = zodToJSONSchema(schema); |
| 124 | + expect(jsonSchema.properties?.map).toMatchObject({ |
| 125 | + type: 'array', |
| 126 | + default: [ |
| 127 | + ['a', 1], |
| 128 | + ['b', 2] |
| 129 | + ] |
| 130 | + }); |
| 131 | + |
| 132 | + const form = await superValidate(zod(schema)); |
| 133 | + expect(form.data.map).toBeInstanceOf(Map); |
| 134 | + expect(form.data.map.size).toBe(2); |
| 135 | + expect(form.data.map.get('a')).toBe(1); |
| 136 | + expect(form.data.map.get('b')).toBe(2); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should allow Set operations on default values', async () => { |
| 140 | + const schema = z.object({ |
| 141 | + tags: z.set(z.string()).default(new Set(['typescript', 'svelte'])) |
| 142 | + }); |
| 143 | + |
| 144 | + const form = await superValidate(zod(schema)); |
| 145 | + |
| 146 | + // This should not throw - the regression would cause this to fail |
| 147 | + expect(() => form.data.tags.has('typescript')).not.toThrow(); |
| 148 | + expect(form.data.tags.has('typescript')).toBe(true); |
| 149 | + expect(form.data.tags.has('svelte')).toBe(true); |
| 150 | + expect(form.data.tags.has('react')).toBe(false); |
| 151 | + }); |
| 152 | +}); |
0 commit comments