Home / File/ zod-error-map.ts — astro Source File

zod-error-map.ts — astro Source File

Architecture documentation for zod-error-map.ts, a typescript file in the astro codebase. 1 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 1 imports 6 functions

Entity Profile

Dependency Diagram

graph LR
  c4702bad_5cd3_4ff4_dfb3_9107bd3ef578["zod-error-map.ts"]
  a2586aae_ce09_613d_4444_659268b61a89["core"]
  c4702bad_5cd3_4ff4_dfb3_9107bd3ef578 --> a2586aae_ce09_613d_4444_659268b61a89
  style c4702bad_5cd3_4ff4_dfb3_9107bd3ef578 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { $ZodErrorMap } from 'zod/v4/core';

type TypeOrLiteralErrByPathEntry = {
	code: 'invalid_type' | 'invalid_literal';
	received: unknown;
	expected: unknown[];
	message: string | undefined;
};

export const errorMap: $ZodErrorMap = (issue) => {
	const baseErrorPath = flattenErrorPath(issue.path ?? []);
	if (issue.code === 'invalid_union') {
		// Optimization: Combine type and literal errors for keys that are common across ALL union types
		// Ex. a union between `{ key: z.literal('tutorial') }` and `{ key: z.literal('blog') }` will
		// raise a single error when `key` does not match:
		// > Did not match union.
		// > key: Expected `'tutorial' | 'blog'`, received 'foo'
		let typeOrLiteralErrByPath = new Map<string, TypeOrLiteralErrByPathEntry>();
		for (const unionError of issue.errors.flat()) {
			if (unionError.code === 'invalid_type') {
				const flattenedErrorPath = flattenErrorPath(unionError.path);
				if (typeOrLiteralErrByPath.has(flattenedErrorPath)) {
					typeOrLiteralErrByPath.get(flattenedErrorPath)!.expected.push(unionError.expected);
				} else {
					typeOrLiteralErrByPath.set(flattenedErrorPath, {
						code: unionError.code,
						received: (unionError as any).received,
						expected: [unionError.expected],
						message: unionError.message,
					});
				}
			}
		}
		const messages: string[] = [prefix(baseErrorPath, 'Did not match union.')];
		const details: string[] = [...typeOrLiteralErrByPath.entries()]
			// If type or literal error isn't common to ALL union types,
			// filter it out. Can lead to confusing noise.
			.filter(([, error]) => error.expected.length === issue.errors.flat().length)
			.map(([key, error]) =>
				key === baseErrorPath
					? // Avoid printing the key again if it's a base error
						`> ${getTypeOrLiteralMsg(error)}`
					: `> ${prefix(key, getTypeOrLiteralMsg(error))}`,
			);

		if (details.length === 0) {
			const expectedShapes: string[] = [];
			for (const unionErrors of issue.errors) {
				const expectedShape: string[] = [];
				for (const _issue of unionErrors) {
					// If the issue is a nested union error, show the associated error message instead of the
					// base error message.
					if (_issue.code === 'invalid_union') {
						return errorMap(_issue as any);
					}
					const relativePath = flattenErrorPath(_issue.path)
						.replace(baseErrorPath, '')
						.replace(leadingPeriod, '');
					if ('expected' in _issue && typeof _issue.expected === 'string') {
						expectedShape.push(
// ... (73 more lines)

Domain

Subdomains

Dependencies

  • core

Frequently Asked Questions

What does zod-error-map.ts do?
zod-error-map.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in zod-error-map.ts?
zod-error-map.ts defines 6 function(s): errorMap, flattenErrorPath, getTypeOrLiteralMsg, prefix, stringify, unionExpectedVals.
What does zod-error-map.ts depend on?
zod-error-map.ts imports 1 module(s): core.
Where is zod-error-map.ts in the architecture?
zod-error-map.ts is located at packages/astro/src/core/errors/zod-error-map.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core/errors).

Analyze Your Own Codebase

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

Try Supermodel Free