|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { formatStandardSchemaIssues } from '../src/standardSchemaIssues' |
| 3 | + |
| 4 | +describe('formatStandardSchemaIssues', () => { |
| 5 | + it('reports a root issue as the bare message', () => { |
| 6 | + expect(formatStandardSchemaIssues([{ message: 'Required' }])).toBe( |
| 7 | + 'Required', |
| 8 | + ) |
| 9 | + }) |
| 10 | + |
| 11 | + it('falls back when there are no issues', () => { |
| 12 | + expect(formatStandardSchemaIssues([])).toBe('Validation failed') |
| 13 | + expect(formatStandardSchemaIssues(undefined)).toBe('Validation failed') |
| 14 | + }) |
| 15 | + |
| 16 | + it('prefixes a nested path', () => { |
| 17 | + expect( |
| 18 | + formatStandardSchemaIssues([ |
| 19 | + { message: 'Expected string', path: ['user', 'name'] }, |
| 20 | + ]), |
| 21 | + ).toBe('user.name: Expected string') |
| 22 | + }) |
| 23 | + |
| 24 | + it('accepts the object form of a path segment', () => { |
| 25 | + expect( |
| 26 | + formatStandardSchemaIssues([ |
| 27 | + { message: 'Expected string', path: [{ key: 'user' }, { key: 'name' }] }, |
| 28 | + ]), |
| 29 | + ).toBe('user.name: Expected string') |
| 30 | + }) |
| 31 | + |
| 32 | + it('renders a numeric key as an index', () => { |
| 33 | + expect( |
| 34 | + formatStandardSchemaIssues([{ message: 'Too small', path: ['items', 0] }]), |
| 35 | + ).toBe('items[0]: Too small') |
| 36 | + }) |
| 37 | + |
| 38 | + it('keeps a numeric string distinct from a numeric index', () => { |
| 39 | + const numeric = formatStandardSchemaIssues([{ message: 'x', path: [0] }]) |
| 40 | + const stringy = formatStandardSchemaIssues([{ message: 'x', path: ['0'] }]) |
| 41 | + expect(numeric).toBe('[0]: x') |
| 42 | + expect(stringy).toBe('["0"]: x') |
| 43 | + expect(numeric).not.toBe(stringy) |
| 44 | + }) |
| 45 | + |
| 46 | + it('does not let a dotted key collide with two keys', () => { |
| 47 | + const twoKeys = formatStandardSchemaIssues([ |
| 48 | + { message: 'x', path: ['a', 'b'] }, |
| 49 | + ]) |
| 50 | + const dottedKey = formatStandardSchemaIssues([ |
| 51 | + { message: 'x', path: ['a.b'] }, |
| 52 | + ]) |
| 53 | + expect(twoKeys).toBe('a.b: x') |
| 54 | + expect(dottedKey).toBe('["a.b"]: x') |
| 55 | + expect(twoKeys).not.toBe(dottedKey) |
| 56 | + }) |
| 57 | + |
| 58 | + it('quotes keys that are not plain identifiers', () => { |
| 59 | + expect( |
| 60 | + formatStandardSchemaIssues([ |
| 61 | + { message: 'x', path: ['with space', 'quote"key', ''] }, |
| 62 | + ]), |
| 63 | + ).toBe('["with space"]["quote\\"key"][""]: x') |
| 64 | + }) |
| 65 | + |
| 66 | + it('handles prototype-named keys without touching the prototype', () => { |
| 67 | + expect( |
| 68 | + formatStandardSchemaIssues([ |
| 69 | + { message: 'x', path: ['__proto__', 'constructor'] }, |
| 70 | + ]), |
| 71 | + ).toBe('__proto__.constructor: x') |
| 72 | + expect(({} as Record<string, unknown>).polluted).toBeUndefined() |
| 73 | + }) |
| 74 | + |
| 75 | + it('renders a symbol key', () => { |
| 76 | + const key = Symbol('secret') |
| 77 | + expect( |
| 78 | + formatStandardSchemaIssues([{ message: 'x', path: [key] }]), |
| 79 | + ).toBe('["Symbol(secret)"]: x') |
| 80 | + }) |
| 81 | + |
| 82 | + it('joins several issues one per line', () => { |
| 83 | + expect( |
| 84 | + formatStandardSchemaIssues([ |
| 85 | + { message: 'Required', path: ['a'] }, |
| 86 | + { message: 'Too long', path: ['b', 1] }, |
| 87 | + ]), |
| 88 | + ).toBe('a: Required\nb[1]: Too long') |
| 89 | + }) |
| 90 | + |
| 91 | + it('survives an issue that cannot be serialized', () => { |
| 92 | + const circular: any = { message: 'Bad input', path: ['a'] } |
| 93 | + circular.self = circular |
| 94 | + expect(() => JSON.stringify(circular)).toThrow() |
| 95 | + expect(formatStandardSchemaIssues([circular])).toBe('a: Bad input') |
| 96 | + }) |
| 97 | + |
| 98 | + it('survives a bigint in the issue, which JSON.stringify refuses', () => { |
| 99 | + const issue: any = { message: 'Bad input', received: 1n } |
| 100 | + expect(() => JSON.stringify(issue)).toThrow() |
| 101 | + expect(formatStandardSchemaIssues([issue])).toBe('Bad input') |
| 102 | + }) |
| 103 | + |
| 104 | + it('survives getters that throw', () => { |
| 105 | + const issue: any = { |
| 106 | + get message() { |
| 107 | + throw new Error('nope') |
| 108 | + }, |
| 109 | + get path() { |
| 110 | + throw new Error('nope') |
| 111 | + }, |
| 112 | + } |
| 113 | + expect(formatStandardSchemaIssues([issue])).toBe('Invalid value') |
| 114 | + }) |
| 115 | + |
| 116 | + it('coerces a non-string message', () => { |
| 117 | + expect(formatStandardSchemaIssues([{ message: 42 } as any])).toBe('42') |
| 118 | + }) |
| 119 | + |
| 120 | + it('skips path segments that carry no usable key', () => { |
| 121 | + expect( |
| 122 | + formatStandardSchemaIssues([ |
| 123 | + { message: 'x', path: ['a', null, undefined, {}, 'b'] as any }, |
| 124 | + ]), |
| 125 | + ).toBe('a.b: x') |
| 126 | + }) |
| 127 | +}) |
0 commit comments