Skip to content
This repository was archived by the owner on May 28, 2026. It is now read-only.

fix: guard against invalid orderRank data to prevent studio crashes#126

Merged
stipsan merged 1 commit into
sanity-io:mainfrom
ChrisNolan:fix/issue-89
Jan 23, 2026
Merged

fix: guard against invalid orderRank data to prevent studio crashes#126
stipsan merged 1 commit into
sanity-io:mainfrom
ChrisNolan:fix/issue-89

Conversation

@ChrisNolan

Copy link
Copy Markdown
Contributor

When an orderRank field contains invalid or corrupted data (non-string values, malformed lexorank strings), the LexoRank parser would throw an unhandled error and crash the studio.

Changes:

  • New: Created parseOrderRank() helper in src/helpers/parseOrderRank.ts that safely parses LexoRank strings with try-catch error handling and
    configurable fallback values
  • Updated: reorderDocuments.ts to use parseOrderRank() when accessing
    rank values from entities
  • Updated: initialRank.ts to use parseOrderRank() when generating
    initial ranks
  • Added: Console warnings when invalid orderRank data is encountered to
    help users identify corrupted documents
  • Tests: Unit tests for parseOrderRank() covering valid parsing, non-string values, and parse failures. Regression tests for reorderDocuments and initialRank with bad data. All 14 tests passing.

Behavior:

  • Invalid orderRank values now gracefully fall back to safe defaults
    (LexoRank.min() or LexoRank.max())
  • Console warnings appear when bad data is encountered:
    [orderable-document-list] Invalid orderRank value...
  • Studio remains stable and functional even with corrupted data
  • Users can query *[_type == "yourType" && !isString(orderRank)] to find and fix corrupted documents

Closes #89

Description

What to review

Testing

When an `orderRank` field contains invalid or corrupted data (non-string
values, malformed lexorank strings), the LexoRank parser would throw an
unhandled error and crash the studio.

Changes:
- New: Created `parseOrderRank()` helper in `src/helpers/parseOrderRank.ts`
  that safely parses LexoRank strings with try-catch error handling and
  configurable fallback values
- Updated: `reorderDocuments.ts` to use `parseOrderRank()` when accessing
  rank values from entities
- Updated: `initialRank.ts` to use `parseOrderRank()` when generating
  initial ranks
- Added: Console warnings when invalid orderRank data is encountered to
  help users identify corrupted documents
- Tests: Unit tests for `parseOrderRank()` covering valid parsing,
  non-string values, and parse failures. Regression tests for
  `reorderDocuments` and `initialRank` with bad data. All 14 tests passing.

Behavior:
- Invalid `orderRank` values now gracefully fall back to safe defaults
  (`LexoRank.min()` or `LexoRank.max()`)
- Console warnings appear when bad data is encountered:
  `[orderable-document-list] Invalid orderRank value...`
- Studio remains stable and functional even with corrupted data
- Users can query `*[_type == "yourType" && !isString(orderRank)]` to
  find and fix corrupted documents

Closes sanity-io#89
@ChrisNolan

Copy link
Copy Markdown
Contributor Author

Note there was code in the DocumentListWrapper to try and stop the display of everything when a string was found -- but it never got there because of 'when' in the load order the plugin was firing.

<code>{ORDER_FIELD_NAME}</code> field on Schema <code>{type}</code> must be{' '}

@stipsan stipsan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This is a great find 💖

@stipsan stipsan merged commit 10b56bc into sanity-io:main Jan 23, 2026
9 checks passed
@ChrisNolan ChrisNolan deleted the fix/issue-89 branch January 25, 2026 00:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sanity AI Assist can fill in an orderRank value and crash the entire application

2 participants