Repository: withastro/astro
File: packages/astro/src/content/runtime.ts (TypeScript)

Code before:
	globalDataStore.get().then((s) => (store = s));
	return function reference(collection: string) {
		return z
			.union([
				z.string(),
				z.object({
					id: z.string(),
					collection: z.string(),
				}),
				z.object({
					slug: z.string(),
					collection: z.string(),
				}),
			])
			.transform(
				(
					lookup:
						| string
						| { id: string; collection: string }
						| { slug: string; collection: string },
					ctx,
				) => {
					if (!store) {
						ctx.addIssue({
							code: ZodIssueCode.custom,
							message: `**${ctx.path.join('.')}:** Reference to ${collection} could not be resolved. Store not available.`,
						});
						return;
					}

					const flattenedErrorPath = ctx.path.join('.');
					const collectionIsInStore = store.hasCollection(collection);

					if (typeof lookup === 'object') {
						// If these don't match then something is wrong with the reference
						if (lookup.collection !== collection) {
							ctx.addIssue({
								code: ZodIssueCode.custom,
								message: `**${flattenedErrorPath}**: Reference to ${collection} invalid. Expected ${collection}. Received ${lookup.collection}.`,
							});
							return;
						}
						// 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.
						// If it is an object then we're validating later in the build, so we can check the collection at that point.

						return lookup;
					}

					if (collectionIsInStore) {
						const entry = store.get(collection, lookup);
						if (!entry) {

Review comment (security):
I would add a more actionable message. If the user encounters this error, what should they do? File an issue? Purge the store? We should provide an escape hatch to our users 

Code after:
	globalDataStore.get().then((s) => (store = s));
	return function reference(collection: string) {
		return z
			.union([
				z.string(),
				z.object({
					id: z.string(),
					collection: z.string(),
				}),
				z.object({
					slug: z.string(),
					collection: z.string(),
				}),
			])
			.transform(
				(
					lookup:
						| string
						| { id: string; collection: string }
						| { slug: string; collection: string },
					ctx,
				) => {
					if (!store) {
						ctx.addIssue({
							code: ZodIssueCode.custom,
							message: `**${ctx.path.join('.')}:** Reference to ${collection} could not be resolved: store not available.\nThis is an Astro bug, so please file an issue at https://github.com/withastro/astro/issues.`,
						});
						return;
					}

					const flattenedErrorPath = ctx.path.join('.');
					const collectionIsInStore = store.hasCollection(collection);

					if (typeof lookup === 'object') {
						// If these don't match then something is wrong with the reference
						if (lookup.collection !== collection) {
							ctx.addIssue({
								code: ZodIssueCode.custom,
								message: `**${flattenedErrorPath}**: Reference to ${collection} invalid. Expected ${collection}. Received ${lookup.collection}.`,
							});
							return;
						}
						// 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.
						// If it is an object then we're validating later in the build, so we can check the collection at that point.

						return lookup;
					}

					if (collectionIsInStore) {
						const entry = store.get(collection, lookup);
						if (!entry) {