From 0327eaac12a6dc22bdbe74fd2326b0a0b737bb70 Mon Sep 17 00:00:00 2001 From: pedrobonamin Date: Sat, 18 Jan 2025 15:31:47 +0100 Subject: [PATCH] fix(core): references to documents on same release are valid --- .../validateDocumentWithReferences.ts | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/sanity/src/core/validation/validateDocumentWithReferences.ts b/packages/sanity/src/core/validation/validateDocumentWithReferences.ts index 2b52c2ab5e8..5ebb2f3fb99 100644 --- a/packages/sanity/src/core/validation/validateDocumentWithReferences.ts +++ b/packages/sanity/src/core/validation/validateDocumentWithReferences.ts @@ -29,11 +29,17 @@ import { shareReplay, skip, throttleTime, + withLatestFrom, } from 'rxjs/operators' import {exhaustMapWithTrailing} from 'rxjs-exhaustmap-with-trailing' import shallowEquals from 'shallow-equals' -import {type DocumentPreviewStore, type LocaleSource, type SourceClientOptions} from '..' +import { + type DocumentPreviewStore, + getVersionFromId, + type LocaleSource, + type SourceClientOptions, +} from '..' import {validateDocumentObservable} from './validateDocument' /** @@ -70,8 +76,11 @@ type GetDocumentExists = NonNullable const listenDocumentExists = ( observeDocumentAvailability: DocumentPreviewStore['unstable_observeDocumentPairAvailability'], id: string, + versionId: string | undefined, ): Observable => - observeDocumentAvailability(id).pipe(map(({published}) => published.available)) + observeDocumentAvailability(id, {version: versionId}).pipe( + map(({published, version}) => published.available || version?.available || false), + ) // throttle delay for referenced document updates (i.e. time between responding to changes in referenced documents) const REF_UPDATE_DELAY = 1000 @@ -98,14 +107,19 @@ export function validateDocumentWithReferences( mergeMap((ids) => from(ids)), ) + const versionId$ = document$.pipe( + map((doc) => getVersionFromId(doc?._id || '')), + distinctUntilChanged(), + ) // Note: we only use this to trigger a re-run of validation when a referenced document is published/unpublished const referenceExistence$ = referenceIds$.pipe( groupBy((id) => id, {duration: () => timer(1000 * 60 * 30)}), mergeMap((id$) => id$.pipe( distinct(), - mergeMap((id) => - listenDocumentExists(ctx.observeDocumentPairAvailability, id).pipe( + withLatestFrom(versionId$), + mergeMap(([id, versionId]) => + listenDocumentExists(ctx.observeDocumentPairAvailability, id, versionId).pipe( map( // eslint-disable-next-line max-nested-callbacks (result) => [id, result] as const,