|
| 1 | +import { describe, expect, it } from 'bun:test' |
| 2 | +import z from 'zod/v4' |
| 3 | + |
| 4 | +import { coerceToArray } from '../utils' |
| 5 | + |
| 6 | +describe('coerceToArray', () => { |
| 7 | + it('passes through arrays unchanged', () => { |
| 8 | + expect(coerceToArray(['a', 'b'])).toEqual(['a', 'b']) |
| 9 | + expect(coerceToArray([{ old: 'x', new: 'y' }])).toEqual([{ old: 'x', new: 'y' }]) |
| 10 | + expect(coerceToArray([])).toEqual([]) |
| 11 | + }) |
| 12 | + |
| 13 | + it('wraps a single string in an array', () => { |
| 14 | + expect(coerceToArray('file.ts')).toEqual(['file.ts']) |
| 15 | + }) |
| 16 | + |
| 17 | + it('wraps a single object in an array', () => { |
| 18 | + expect(coerceToArray({ old: 'x', new: 'y' })).toEqual([{ old: 'x', new: 'y' }]) |
| 19 | + }) |
| 20 | + |
| 21 | + it('wraps a single number in an array', () => { |
| 22 | + expect(coerceToArray(42)).toEqual([42]) |
| 23 | + }) |
| 24 | + |
| 25 | + it('parses a stringified JSON array', () => { |
| 26 | + expect(coerceToArray('["file1.ts", "file2.ts"]')).toEqual(['file1.ts', 'file2.ts']) |
| 27 | + }) |
| 28 | + |
| 29 | + it('wraps a non-JSON string (does not parse as array)', () => { |
| 30 | + expect(coerceToArray('not-json')).toEqual(['not-json']) |
| 31 | + }) |
| 32 | + |
| 33 | + it('wraps a stringified JSON object (not an array) in an array', () => { |
| 34 | + expect(coerceToArray('{"key": "value"}')).toEqual(['{"key": "value"}']) |
| 35 | + }) |
| 36 | + |
| 37 | + it('passes through null', () => { |
| 38 | + expect(coerceToArray(null)).toBeNull() |
| 39 | + }) |
| 40 | + |
| 41 | + it('passes through undefined', () => { |
| 42 | + expect(coerceToArray(undefined)).toBeUndefined() |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +describe('coerceToArray with Zod schemas', () => { |
| 47 | + it('coerces a single string into an array for z.array(z.string())', () => { |
| 48 | + const schema = z.object({ |
| 49 | + paths: z.preprocess(coerceToArray, z.array(z.string())), |
| 50 | + }) |
| 51 | + const result = schema.safeParse({ paths: 'file.ts' }) |
| 52 | + expect(result.success).toBe(true) |
| 53 | + if (result.success) { |
| 54 | + expect(result.data.paths).toEqual(['file.ts']) |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + it('coerces a single object into an array for z.array(z.object(...))', () => { |
| 59 | + const schema = z.object({ |
| 60 | + replacements: z.preprocess( |
| 61 | + coerceToArray, |
| 62 | + z.array(z.object({ old: z.string(), new: z.string() })), |
| 63 | + ), |
| 64 | + }) |
| 65 | + const result = schema.safeParse({ replacements: { old: 'x', new: 'y' } }) |
| 66 | + expect(result.success).toBe(true) |
| 67 | + if (result.success) { |
| 68 | + expect(result.data.replacements).toEqual([{ old: 'x', new: 'y' }]) |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + it('still validates correctly when already an array', () => { |
| 73 | + const schema = z.object({ |
| 74 | + paths: z.preprocess(coerceToArray, z.array(z.string())), |
| 75 | + }) |
| 76 | + const result = schema.safeParse({ paths: ['a.ts', 'b.ts'] }) |
| 77 | + expect(result.success).toBe(true) |
| 78 | + if (result.success) { |
| 79 | + expect(result.data.paths).toEqual(['a.ts', 'b.ts']) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + it('still rejects invalid inner types after coercion', () => { |
| 84 | + const schema = z.object({ |
| 85 | + paths: z.preprocess(coerceToArray, z.array(z.string())), |
| 86 | + }) |
| 87 | + const result = schema.safeParse({ paths: 123 }) |
| 88 | + expect(result.success).toBe(false) |
| 89 | + }) |
| 90 | + |
| 91 | + it('works with optional arrays', () => { |
| 92 | + const schema = z.object({ |
| 93 | + paths: z.preprocess(coerceToArray, z.array(z.string())).optional(), |
| 94 | + }) |
| 95 | + const withValue = schema.safeParse({ paths: 'file.ts' }) |
| 96 | + expect(withValue.success).toBe(true) |
| 97 | + if (withValue.success) { |
| 98 | + expect(withValue.data.paths).toEqual(['file.ts']) |
| 99 | + } |
| 100 | + |
| 101 | + const withoutValue = schema.safeParse({}) |
| 102 | + expect(withoutValue.success).toBe(true) |
| 103 | + if (withoutValue.success) { |
| 104 | + expect(withoutValue.data.paths).toBeUndefined() |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + it('produces identical JSON schema with or without preprocess', () => { |
| 109 | + const plain = z.object({ paths: z.array(z.string()) }) |
| 110 | + const coerced = z.object({ |
| 111 | + paths: z.preprocess(coerceToArray, z.array(z.string())), |
| 112 | + }) |
| 113 | + |
| 114 | + const plainSchema = z.toJSONSchema(plain, { io: 'input' }) |
| 115 | + const coercedSchema = z.toJSONSchema(coerced, { io: 'input' }) |
| 116 | + expect(coercedSchema).toEqual(plainSchema) |
| 117 | + }) |
| 118 | +}) |
0 commit comments