@@ -161,15 +161,25 @@ export function zodParseWithContext<T extends z.ZodType>(
161161 // <-- Add handling for invalid_type here -->
162162 else if ( issue . code === 'invalid_type' ) {
163163 const received = issue . received === 'undefined' ? 'missing' : issue . received
164- // Get the actual value for context
165- const actualValue = getByPath ( parentObj , nestedPath ) // Get value from parent context
164+ const actualValue = getByPath ( parentObj , nestedPath )
166165 const actualValueStr =
167166 typeof actualValue === 'object' && actualValue !== null
168167 ? JSON . stringify ( actualValue )
169168 : String ( actualValue )
170- // Simple message not relying on issue.expected
169+
170+ let expectedOutput = String ( issue . expected )
171+ const MAX_EXPECTED_TO_SHOW = 3
172+ if ( typeof issue . expected === 'string' && issue . expected . includes ( ' | ' ) ) {
173+ const expectedValues = issue . expected . split ( ' | ' )
174+ if ( expectedValues . length > MAX_EXPECTED_TO_SHOW ) {
175+ const shownValues = expectedValues . slice ( 0 , MAX_EXPECTED_TO_SHOW ) . join ( ' | ' )
176+ const remainingCount = expectedValues . length - MAX_EXPECTED_TO_SHOW
177+ expectedOutput = `${ shownValues } | .. or ${ remainingCount } others ..`
178+ }
179+ }
180+
171181 collectedMessages [ nestedPath ] . push (
172- `got invalid type: ${ received } (value: \`${ actualValueStr } \`, expected: ${ issue . expected } )` ,
182+ `got invalid type: ${ received } (value: \`${ actualValueStr } \`, expected: ${ expectedOutput } )` ,
173183 )
174184 }
175185 // <-- End added handling -->
@@ -202,7 +212,7 @@ export function zodParseWithContext<T extends z.ZodType>(
202212 targetMessages . push ( ...unrecognizedKeyMessages )
203213 } else if ( literalMessages . length > 0 ) {
204214 const uniqueLiterals = [ ...new Set ( literalMessages ) ]
205- targetMessages . push ( `should be one of: \`${ uniqueLiterals . join ( '`, `' ) } \`` )
215+ targetMessages . push ( `should be one of: \`${ uniqueLiterals . join ( '\ `, \ `' ) } \`` )
206216 } else {
207217 // Fallback to joining the collected raw messages for this path
208218 targetMessages . push ( ...collectedMessages [ nestedPath ] )
0 commit comments