|
| 1 | +import { z } from 'zod'; |
| 2 | +import { smartUnion } from '../../src/zod-utils'; |
| 3 | + |
| 4 | +describe('Zod smart union', () => { |
| 5 | + it('should work with scalar union', () => { |
| 6 | + const schema = smartUnion(z, [z.string(), z.number()]); |
| 7 | + expect(schema.safeParse('test')).toMatchObject({ success: true, data: 'test' }); |
| 8 | + expect(schema.safeParse(1)).toMatchObject({ success: true, data: 1 }); |
| 9 | + expect(schema.safeParse(true)).toMatchObject({ success: false }); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should work with non-ambiguous object union', () => { |
| 13 | + const schema = smartUnion(z, [z.object({ a: z.string() }), z.object({ b: z.number() }).strict()]); |
| 14 | + expect(schema.safeParse({ a: 'test' })).toMatchObject({ success: true, data: { a: 'test' } }); |
| 15 | + expect(schema.safeParse({ b: 1 })).toMatchObject({ success: true, data: { b: 1 } }); |
| 16 | + expect(schema.safeParse({ a: 'test', b: 1 })).toMatchObject({ success: true }); |
| 17 | + expect(schema.safeParse({ b: 1, c: 'test' })).toMatchObject({ success: false }); |
| 18 | + expect(schema.safeParse({ c: 'test' })).toMatchObject({ success: false }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should work with ambiguous object union', () => { |
| 22 | + const schema = smartUnion(z, [ |
| 23 | + z.object({ a: z.string(), b: z.number() }), |
| 24 | + z.object({ a: z.string(), c: z.boolean() }), |
| 25 | + ]); |
| 26 | + expect(schema.safeParse({ a: 'test', b: 1 })).toMatchObject({ success: true, data: { a: 'test', b: 1 } }); |
| 27 | + expect(schema.safeParse({ a: 'test', c: true })).toMatchObject({ success: true, data: { a: 'test', c: true } }); |
| 28 | + expect(schema.safeParse({ a: 'test', b: 1, z: 'z' })).toMatchObject({ |
| 29 | + success: true, |
| 30 | + data: { a: 'test', b: 1 }, |
| 31 | + }); |
| 32 | + expect(schema.safeParse({ a: 'test', c: true, z: 'z' })).toMatchObject({ |
| 33 | + success: true, |
| 34 | + data: { a: 'test', c: true }, |
| 35 | + }); |
| 36 | + expect(schema.safeParse({ c: 'test' })).toMatchObject({ success: false }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should work with non-ambiguous array union', () => { |
| 40 | + const schema = smartUnion(z, [ |
| 41 | + z.object({ a: z.string() }).array(), |
| 42 | + z.object({ b: z.number() }).strict().array(), |
| 43 | + ]); |
| 44 | + |
| 45 | + expect(schema.safeParse([{ a: 'test' }])).toMatchObject({ success: true, data: [{ a: 'test' }] }); |
| 46 | + expect(schema.safeParse([{ a: 'test' }, { a: 'test1' }])).toMatchObject({ |
| 47 | + success: true, |
| 48 | + data: [{ a: 'test' }, { a: 'test1' }], |
| 49 | + }); |
| 50 | + |
| 51 | + expect(schema.safeParse([{ b: 1 }])).toMatchObject({ success: true, data: [{ b: 1 }] }); |
| 52 | + expect(schema.safeParse([{ a: 'test', b: 1 }])).toMatchObject({ success: true }); |
| 53 | + expect(schema.safeParse([{ b: 1, c: 'test' }])).toMatchObject({ success: false }); |
| 54 | + expect(schema.safeParse([{ c: 'test' }])).toMatchObject({ success: false }); |
| 55 | + |
| 56 | + // all items must match the same candidate |
| 57 | + expect(schema.safeParse([{ a: 'test' }, { b: 1 }])).toMatchObject({ success: false }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should work with ambiguous array union', () => { |
| 61 | + const schema = smartUnion(z, [ |
| 62 | + z.object({ a: z.string(), b: z.number() }).array(), |
| 63 | + z.object({ a: z.string(), c: z.boolean() }).array(), |
| 64 | + ]); |
| 65 | + |
| 66 | + expect(schema.safeParse([{ a: 'test', b: 1 }])).toMatchObject({ success: true, data: [{ a: 'test', b: 1 }] }); |
| 67 | + expect(schema.safeParse([{ a: 'test', c: true }])).toMatchObject({ |
| 68 | + success: true, |
| 69 | + data: [{ a: 'test', c: true }], |
| 70 | + }); |
| 71 | + expect(schema.safeParse([{ a: 'test', b: 1, z: 'z' }])).toMatchObject({ |
| 72 | + success: true, |
| 73 | + data: [{ a: 'test', b: 1 }], |
| 74 | + }); |
| 75 | + expect(schema.safeParse([{ a: 'test', c: true, z: 'z' }])).toMatchObject({ |
| 76 | + success: true, |
| 77 | + data: [{ a: 'test', c: true }], |
| 78 | + }); |
| 79 | + expect(schema.safeParse([{ c: 'test' }])).toMatchObject({ success: false }); |
| 80 | + |
| 81 | + // all items must match the same candidate |
| 82 | + expect(schema.safeParse([{ a: 'test' }, { c: true }])).toMatchObject({ success: false }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should work with lazy schemas', () => { |
| 86 | + const schema = smartUnion(z, [ |
| 87 | + z.lazy(() => z.object({ a: z.string(), b: z.number() })), |
| 88 | + z.lazy(() => z.object({ a: z.string(), c: z.boolean() })), |
| 89 | + ]); |
| 90 | + expect(schema.safeParse({ a: 'test', b: 1 })).toMatchObject({ success: true, data: { a: 'test', b: 1 } }); |
| 91 | + expect(schema.safeParse({ a: 'test', c: true })).toMatchObject({ success: true, data: { a: 'test', c: true } }); |
| 92 | + expect(schema.safeParse({ a: 'test', b: 1, z: 'z' })).toMatchObject({ |
| 93 | + success: true, |
| 94 | + data: { a: 'test', b: 1 }, |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should work with mixed object and array unions', () => { |
| 99 | + const schema = smartUnion(z, [ |
| 100 | + z.object({ a: z.string() }).strict(), |
| 101 | + z.object({ b: z.number() }).strict().array(), |
| 102 | + ]); |
| 103 | + |
| 104 | + expect(schema.safeParse({ a: 'test' })).toMatchObject({ success: true, data: { a: 'test' } }); |
| 105 | + expect(schema.safeParse([{ b: 1 }])).toMatchObject({ success: true, data: [{ b: 1 }] }); |
| 106 | + expect(schema.safeParse({ a: 'test', b: 1 })).toMatchObject({ success: false }); |
| 107 | + expect(schema.safeParse([{ a: 'test' }])).toMatchObject({ success: false }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments