Skip to content

Commit 11d447f

Browse files
authoredOct 1, 2024··
fix: defer check for schema references to content layer collections (#12097)
* fix: defer check for schema references * More comments
1 parent f06feee commit 11d447f

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed
 

‎.changeset/bright-swans-shout.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes error where references in content layer schemas sometimes incorrectly report as missing

‎packages/astro/src/content/runtime.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -599,16 +599,9 @@ export function createReference({ lookupMap }: { lookupMap: ContentLookupMap })
599599
});
600600
return;
601601
}
602+
// We won't throw if the collection is missing, because it may be a content layer collection and the store may not yet be populated.
603+
// If it is an object then we're validating later in the build, so we can check the collection at that point.
602604

603-
// A reference object might refer to an invalid collection, because when we convert it we don't have access to the store.
604-
// If it is an object then we're validating later in the pipeline, so we can check the collection at that point.
605-
if (!lookupMap[collection] && !collectionIsInStore) {
606-
ctx.addIssue({
607-
code: ZodIssueCode.custom,
608-
message: `**${flattenedErrorPath}:** Reference to ${collection} invalid. Collection does not exist or is empty.`,
609-
});
610-
return;
611-
}
612605
return lookup;
613606
}
614607

@@ -623,9 +616,10 @@ export function createReference({ lookupMap }: { lookupMap: ContentLookupMap })
623616
}
624617
return { id: lookup, collection };
625618
}
626-
627-
if (!lookupMap[collection] && store.collections().size === 0) {
628-
// If the collection is not in the lookup map or store, it may be a content layer collection and the store may not yet be populated.
619+
// If the collection is not in the lookup map or store, it may be a content layer collection and the store may not yet be populated.
620+
// If the store has 0 or 1 entries it probably means that the entries have not yet been loaded.
621+
// The store may have a single entry even if the collections have not loaded, because the top-level metadata collection is generated early.
622+
if (!lookupMap[collection] && store.collections().size <= 1) {
629623
// For now, we can't validate this reference, so we'll optimistically convert it to a reference object which we'll validate
630624
// later in the pipeline when we do have access to the store.
631625
return { id: lookup, collection };

0 commit comments

Comments
 (0)
Please sign in to comment.