Skip to content

fix(documents): fixes documents validate not working#386

Merged
binoy14 merged 4 commits intomainfrom
sdk-756
Feb 5, 2026
Merged

fix(documents): fixes documents validate not working#386
binoy14 merged 4 commits intomainfrom
sdk-756

Conversation

@binoy14
Copy link
Contributor

@binoy14 binoy14 commented Feb 4, 2026

TL;DR

Refactored document validation to use studio workers, improving reliability and maintainability.

What changed?

  • Moved validation logic from a custom worker thread to use the standard studio worker infrastructure
  • Relocated validateDocuments.ts to a worker file with .worker.ts extension
  • Created a new documents.ts service to handle document-related API operations
  • Added support for directory paths in getStudioWorkspaces
  • Improved error handling and reporting in the validation command
  • Added comprehensive tests for document validation functionality
  • Added test fixtures for valid and invalid documents

How to test?

  1. Run the validate command with a file containing valid documents:

    sanity documents validate --file valid-documents.ndjson --format json
    
  2. Run the validate command with a file containing invalid documents:

    sanity documents validate --file invalid-documents.ndjson --format ndjson
    
  3. Test with different level filters:

    sanity documents validate --file invalid-documents.ndjson --level error
    
  4. Test with a multi-workspace studio:

    sanity documents validate --workspace production --file valid-documents.ndjson
    

Why make this change?

This refactoring leverages the existing studio worker infrastructure instead of using a custom worker implementation, making the code more maintainable and consistent with other parts of the codebase. The changes also improve error handling and provide better testing coverage for document validation functionality. By extracting document-related operations into a dedicated service, the code is more modular and easier to maintain.

@linear
Copy link

linear bot commented Feb 4, 2026

@claude
Copy link

claude bot commented Feb 4, 2026

Claude finished @binoy14's task in 1m 48s —— View job


Code Review Complete

Reviewed PR #386: fix(documents): fixes documents validate not working

Issues Found

1. API Version Mismatch Between Services

The documents.ts service uses API version v2021-03-25 (line 3), but constants.ts defines DOCUMENTS_API_VERSION = '2025-08-15'. While validate.ts imports from constants.ts, the worker imports from documents.ts, creating inconsistency.

Recommendation: Use a single source of truth for the API version. Either both should use the constant or the constant should be removed.

  • packages/@sanity/cli/src/services/documents.ts:3
  • packages/@sanity/cli/src/actions/documents/constants.ts:1

2. Missing Error Response Validation

exportDocuments doesn't check the HTTP response status before returning. A failed export (4xx/5xx) would return a Response object with an error body instead of document data.

Fix: Add response validation:

if (!response.ok) {
  throw new Error(`Export failed: ${response.status} ${response.statusText}`)
}
  • packages/@sanity/cli/src/services/documents.ts:50-64

3. Worker Exit Logic Allows Silent Failures

In studioWorkerTask.ts:54-57, the exit listener only rejects on code > 0, but doesn't handle the cleanup properly. If the worker exits with code 0 before sending a message, the promise hangs until setImmediate cleanup fires, potentially causing timeout issues.

Recommendation: Track whether a message was received and reject if worker exits without sending one.

  • packages/@sanity/cli-core/src/loaders/studio/studioWorkerTask.ts:54-57

4. Unhandled Stream Reader Error

readerToGenerator in the worker doesn't handle potential errors from reader.read(). If the stream errors mid-read, it will bubble up unhandled.

Fix: Wrap in try-catch or let the caller handle the error propagation explicitly.

  • packages/@sanity/cli/src/actions/documents/validateDocuments.worker.ts:53-59

5. Potential Memory Leak with Temp File

If validation throws after creating the temp file but before cleanup runs, the temp file won't be deleted. The finally block at line 332 will call cleanup, but if an error occurs during file writing (lines 120-138), cleanup is undefined and the file persists.

Fix: Set cleanupDownloadedDocuments immediately after creating the temp file path, even before writing starts.

  • packages/@sanity/cli/src/actions/documents/validateDocuments.worker.ts:217-333

6. Low Test Coverage

Coverage report shows several files with inadequate coverage:

  • prettyReporter.ts: 6.66% (lines 25-124 uncovered)
  • validate.ts (action): 54.54% (lines 32-47 uncovered)
  • getStudioWorkspaces.ts: 0% (lines 21-51 uncovered)

Recommendation: Add tests for error paths and reporter formatting, especially since prettyReporter is the default.

7. Type Safety Issue in Error Handling

Worker error handling uses isRecord utility but doesn't validate the message property is actually a string before using it. While the check is there, TypeScript doesn't narrow the type correctly.

Fix: Use a type guard or explicit check:

const errorMessage = err instanceof Error ? err.message : String(err)
  • packages/@sanity/cli/src/actions/documents/validateDocuments.worker.ts:291-293

Copy link
Contributor Author

binoy14 commented Feb 4, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions
Copy link

github-actions bot commented Feb 4, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 77.23% 6901 / 8935
🔵 Statements 76.73% 7113 / 9269
🔵 Functions 73.75% 1180 / 1600
🔵 Branches 63.58% 3182 / 5004
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/@sanity/cli-core/src/config/studio/getStudioWorkspaces.ts 0% 0% 0% 0% 21-51
packages/@sanity/cli-core/src/loaders/studio/studioWorkerTask.ts 84.21% 50% 88.88% 83.33% 60-61, 107
packages/@sanity/cli/src/actions/documents/validate.ts 54.54% 0% 33.33% 54.54% 32-47
packages/@sanity/cli/src/actions/documents/validation/reporters/jsonReporter.ts 72.22% 0% 75% 80% 18, 27-28
packages/@sanity/cli/src/actions/documents/validation/reporters/ndjsonReporter.ts 91.66% 75% 100% 100% 12
packages/@sanity/cli/src/actions/documents/validation/reporters/prettyReporter/prettyReporter.ts 6.66% 4.16% 20% 7.4% 25-124
packages/@sanity/cli/src/commands/documents/validate.ts 100% 83.33% 100% 100%
packages/@sanity/cli/src/services/documents.ts 100% 100% 100% 100%
Generated in workflow #2021 for commit e69c54f by the Vitest Coverage Report Action

@binoy14 binoy14 marked this pull request as ready for review February 4, 2026 17:31
@binoy14 binoy14 requested a review from a team as a code owner February 4, 2026 17:31
@binoy14 binoy14 requested review from rexxars and removed request for a team February 4, 2026 17:31
rexxars
rexxars previously approved these changes Feb 4, 2026
Copy link
Member

@rexxars rexxars left a comment

Choose a reason for hiding this comment

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

Nice refactoring, I like this.

@binoy14
Copy link
Contributor Author

binoy14 commented Feb 5, 2026

Couple PRs touching similar files so merge conflicts

@binoy14 binoy14 merged commit 9a3337b into main Feb 5, 2026
34 checks passed
@binoy14 binoy14 deleted the sdk-756 branch February 5, 2026 16:55
@squiggler-legacy squiggler-legacy bot mentioned this pull request Feb 5, 2026
This was referenced Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants