|
1 | | -import {parseHumanReadableError} from './error-parsing.js' |
| 1 | +import {parseHumanReadableError, parseStructuredErrors} from './error-parsing.js' |
2 | 2 | import {describe, expect, test} from 'vitest' |
3 | 3 |
|
4 | 4 | describe('parseHumanReadableError', () => { |
@@ -235,3 +235,150 @@ describe('parseHumanReadableError', () => { |
235 | 235 | expect(result).not.toContain('Union validation failed') |
236 | 236 | }) |
237 | 237 | }) |
| 238 | + |
| 239 | +describe('parseStructuredErrors', () => { |
| 240 | + test('preserves regular issues with file path and path string', () => { |
| 241 | + const issues = [ |
| 242 | + { |
| 243 | + path: ['name'], |
| 244 | + message: 'Required field is missing', |
| 245 | + code: 'invalid_type', |
| 246 | + }, |
| 247 | + { |
| 248 | + path: ['version'], |
| 249 | + message: 'Must be a valid semver string', |
| 250 | + code: 'custom', |
| 251 | + }, |
| 252 | + ] |
| 253 | + |
| 254 | + const result = parseStructuredErrors(issues, '/tmp/shopify.app.toml') |
| 255 | + |
| 256 | + expect(result).toEqual([ |
| 257 | + { |
| 258 | + filePath: '/tmp/shopify.app.toml', |
| 259 | + path: ['name'], |
| 260 | + pathString: 'name', |
| 261 | + message: 'Required field is missing', |
| 262 | + code: 'invalid_type', |
| 263 | + }, |
| 264 | + { |
| 265 | + filePath: '/tmp/shopify.app.toml', |
| 266 | + path: ['version'], |
| 267 | + pathString: 'version', |
| 268 | + message: 'Must be a valid semver string', |
| 269 | + code: 'custom', |
| 270 | + }, |
| 271 | + ]) |
| 272 | + }) |
| 273 | + |
| 274 | + test('uses the best matching union variant for structured issues', () => { |
| 275 | + const issues = [ |
| 276 | + { |
| 277 | + code: 'invalid_union', |
| 278 | + unionErrors: [ |
| 279 | + { |
| 280 | + issues: [ |
| 281 | + { |
| 282 | + code: 'invalid_type', |
| 283 | + path: ['web', 'roles'], |
| 284 | + message: 'Expected array, received number', |
| 285 | + }, |
| 286 | + { |
| 287 | + code: 'invalid_type', |
| 288 | + path: ['web', 'commands', 'build'], |
| 289 | + message: 'Required', |
| 290 | + }, |
| 291 | + ], |
| 292 | + name: 'ZodError', |
| 293 | + }, |
| 294 | + { |
| 295 | + issues: [ |
| 296 | + { |
| 297 | + code: 'invalid_literal', |
| 298 | + path: ['web', 'type'], |
| 299 | + message: "Invalid literal value, expected 'frontend'", |
| 300 | + }, |
| 301 | + ], |
| 302 | + name: 'ZodError', |
| 303 | + }, |
| 304 | + ], |
| 305 | + path: ['web'], |
| 306 | + message: 'Invalid input', |
| 307 | + }, |
| 308 | + ] |
| 309 | + |
| 310 | + const result = parseStructuredErrors(issues, '/tmp/shopify.web.toml') |
| 311 | + |
| 312 | + expect(result).toEqual([ |
| 313 | + { |
| 314 | + filePath: '/tmp/shopify.web.toml', |
| 315 | + path: ['web', 'roles'], |
| 316 | + pathString: 'web.roles', |
| 317 | + message: 'Expected array, received number', |
| 318 | + code: 'invalid_type', |
| 319 | + }, |
| 320 | + { |
| 321 | + filePath: '/tmp/shopify.web.toml', |
| 322 | + path: ['web', 'commands', 'build'], |
| 323 | + pathString: 'web.commands.build', |
| 324 | + message: 'Required', |
| 325 | + code: 'invalid_type', |
| 326 | + }, |
| 327 | + ]) |
| 328 | + }) |
| 329 | + |
| 330 | + test('falls back to recovered nested union issues before returning a synthetic root issue', () => { |
| 331 | + const unionIssues = Array.from({length: 51}, (_, index) => ({ |
| 332 | + code: 'custom', |
| 333 | + path: ['variants', index], |
| 334 | + message: `Invalid variant ${index + 1}`, |
| 335 | + })) |
| 336 | + |
| 337 | + const issues = [ |
| 338 | + { |
| 339 | + code: 'invalid_union', |
| 340 | + unionErrors: [{issues: unionIssues, name: 'ZodError'}], |
| 341 | + path: ['root'], |
| 342 | + message: 'Invalid input', |
| 343 | + }, |
| 344 | + ] |
| 345 | + |
| 346 | + const result = parseStructuredErrors(issues, '/tmp/shopify.app.toml') |
| 347 | + |
| 348 | + expect(result).toEqual( |
| 349 | + unionIssues.map((issue, index) => ({ |
| 350 | + filePath: '/tmp/shopify.app.toml', |
| 351 | + path: ['variants', index], |
| 352 | + pathString: `variants.${index}`, |
| 353 | + message: issue.message, |
| 354 | + code: 'custom', |
| 355 | + })), |
| 356 | + ) |
| 357 | + }) |
| 358 | + |
| 359 | + test('falls back to a synthetic root issue when union variants expose no nested issues', () => { |
| 360 | + const issues = [ |
| 361 | + { |
| 362 | + code: 'invalid_union', |
| 363 | + unionErrors: [ |
| 364 | + {issues: [], name: 'ZodError'}, |
| 365 | + {issues: [], name: 'ZodError'}, |
| 366 | + ], |
| 367 | + path: ['root'], |
| 368 | + message: 'Invalid input', |
| 369 | + }, |
| 370 | + ] |
| 371 | + |
| 372 | + const result = parseStructuredErrors(issues, '/tmp/shopify.app.toml') |
| 373 | + |
| 374 | + expect(result).toEqual([ |
| 375 | + { |
| 376 | + filePath: '/tmp/shopify.app.toml', |
| 377 | + path: ['root'], |
| 378 | + pathString: 'root', |
| 379 | + message: "Configuration doesn't match any expected format", |
| 380 | + code: 'invalid_union', |
| 381 | + }, |
| 382 | + ]) |
| 383 | + }) |
| 384 | +}) |
0 commit comments