Home / Function/ generateJSONSchema() — astro Function Reference

generateJSONSchema() — astro Function Reference

Architecture documentation for the generateJSONSchema() function in types-generator.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  e8e4a435_135c_9f53_dc21_719f629aad4a["generateJSONSchema()"]
  eca81928_a18b_c02e_8e5a_0440befa1a98["types-generator.ts"]
  e8e4a435_135c_9f53_dc21_719f629aad4a -->|defined in| eca81928_a18b_c02e_8e5a_0440befa1a98
  453daca9_4232_0b6c_8607_0e08e829d051["writeContentFiles()"]
  453daca9_4232_0b6c_8607_0e08e829d051 -->|calls| e8e4a435_135c_9f53_dc21_719f629aad4a
  b10d9005_fa71_f3ce_7f9d_49b77a03fefd["getContentLayerSchema()"]
  e8e4a435_135c_9f53_dc21_719f629aad4a -->|calls| b10d9005_fa71_f3ce_7f9d_49b77a03fefd
  style e8e4a435_135c_9f53_dc21_719f629aad4a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/types-generator.ts lines 606–665

async function generateJSONSchema(
	fsMod: typeof import('node:fs'),
	collectionConfig: CollectionConfig,
	collectionKey: string,
	collectionSchemasDir: URL,
	logger: Logger,
) {
	let zodSchemaForJson =
		typeof collectionConfig.schema === 'function'
			? collectionConfig.schema({ image: () => z.string() })
			: collectionConfig.schema;

	if (!zodSchemaForJson && collectionConfig.type === CONTENT_LAYER_TYPE) {
		zodSchemaForJson = await getContentLayerSchema(collectionConfig, collectionKey);
	}

	// The `file()` loader uses a schema which applies to every item in the file rather than a schema
	// for the whole file. We special case this to provide the correct JSON schema to users.
	// TODO: it would be nice if loaders could indicate this behavior so it wasn’t unique to the built-in loader.
	if (
		collectionConfig.type === CONTENT_LAYER_TYPE &&
		collectionConfig.loader.name === 'file-loader'
	) {
		// `file()` supports arrays of items, but you can’t set `$schema` when using a top-level array,
		// so we’re only handling the object case.
		// We use `z.object()` instead of `z.record()` for compatibility with the next `if` statement.
		zodSchemaForJson = z.object({}).catchall(zodSchemaForJson);
	}

	if (zodSchemaForJson instanceof z.ZodObject) {
		zodSchemaForJson = zodSchemaForJson.extend({
			$schema: z.string().optional(),
		});
	}

	try {
		const schema = z.toJSONSchema(zodSchemaForJson, {
			unrepresentable: 'any',
			override: (ctx) => {
				const def = ctx.zodSchema._zod.def;
				if (def.type === 'date') {
					ctx.jsonSchema.type = 'string';
					ctx.jsonSchema.format = 'date-time';
				}
			},
		});
		const schemaStr = JSON.stringify(schema, null, 2);
		const schemaJsonPath = new URL(
			`./${collectionKey.replace(/"/g, '')}.schema.json`,
			collectionSchemasDir,
		);
		await fsMod.promises.writeFile(schemaJsonPath, schemaStr);
	} catch (err) {
		// This should error gracefully and not crash the dev server
		logger.warn(
			'content',
			`An error was encountered while creating the JSON schema for the ${collectionKey} collection. Proceeding without it. Error: ${err}`,
		);
	}
}

Subdomains

Frequently Asked Questions

What does generateJSONSchema() do?
generateJSONSchema() is a function in the astro codebase, defined in packages/astro/src/content/types-generator.ts.
Where is generateJSONSchema() defined?
generateJSONSchema() is defined in packages/astro/src/content/types-generator.ts at line 606.
What does generateJSONSchema() call?
generateJSONSchema() calls 1 function(s): getContentLayerSchema.
What calls generateJSONSchema()?
generateJSONSchema() is called by 1 function(s): writeContentFiles.

Analyze Your Own Codebase

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

Try Supermodel Free