fix(plugin-ecommerce): prevent crash when variant.options is undefined by Gap0507 · Pull Request #16372 · payloadcms/payload · GitHub
Skip to content

fix(plugin-ecommerce): prevent crash when variant.options is undefined#16372

Open
Gap0507 wants to merge 1 commit intopayloadcms:mainfrom
Gap0507:fix/validate-options-null-guard
Open

fix(plugin-ecommerce): prevent crash when variant.options is undefined#16372
Gap0507 wants to merge 1 commit intopayloadcms:mainfrom
Gap0507:fix/validate-options-null-guard

Conversation

@Gap0507
Copy link
Copy Markdown

@Gap0507 Gap0507 commented Apr 23, 2026

What?

Fixes a runtime crash in the validateOptions hook of the ecommerce plugin when a variant has options set to undefined or null.

Why?

The current implementation assumes that every variant has options as an array. In real-world scenarios, this is not always true due to:

  • Legacy data (before the options field existed)
  • Partially migrated data
  • Direct database inserts bypassing validation

When such a variant is encountered, the code attempts to access .length on a non-array value, resulting in a TypeError and blocking all variant creation/updates for that product.

How?

Added defensive checks using Array.isArray():

  • While collecting existing variant options, only valid arrays are considered
  • During duplicate comparison, an additional guard ensures safety

This approach:

  • Prevents crashes
  • Maintains correct duplicate detection logic
  • Safely ignores invalid/legacy data without mutating it

Fixes #16368

…lidation crash

Skip variants with non-array options to avoid TypeError when accessing length. Ensures backward compatibility with legacy or corrupted data.
@Gap0507 Gap0507 changed the title fix(ecommerce): prevent crash when variant.options is undefined fix(plugin-ecommerce): prevent crash when variant.options is undefined Apr 23, 2026
@denolfe denolfe added the v3 label Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[plugin-ecommerce] validateOptions crashes when a sibling variant has no options

2 participants