Home / Function/ createReference() — astro Function Reference

createReference() — astro Function Reference

Architecture documentation for the createReference() function in runtime.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  00c9f371_a312_e668_56af_d370bebf639f["createReference()"]
  73d13646_8e80_972f_3adc_f28448b64e4d["runtime.ts"]
  00c9f371_a312_e668_56af_d370bebf639f -->|defined in| 73d13646_8e80_972f_3adc_f28448b64e4d
  style 00c9f371_a312_e668_56af_d370bebf639f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/runtime.ts lines 659–692

export function createReference() {
	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, ctx) => {
				if (typeof lookup === 'object') {
					// If these don't match then something is wrong with the reference
					if (lookup.collection !== collection) {
						const flattenedErrorPath = ctx.issues[0]?.path?.join('.');

						ctx.addIssue({
							code: 'custom',
							message: `**${flattenedErrorPath}**: Reference to ${collection} invalid. Expected ${collection}. Received ${lookup.collection}.`,
						});
						return;
					}
					// If it is an object then we're validating later in the build, so we can check the collection at that point.
					return lookup;
				}

				return { id: lookup, collection };
			});
	};
}

Subdomains

Frequently Asked Questions

What does createReference() do?
createReference() is a function in the astro codebase, defined in packages/astro/src/content/runtime.ts.
Where is createReference() defined?
createReference() is defined in packages/astro/src/content/runtime.ts at line 659.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free