errorMap() — astro Function Reference
Architecture documentation for the errorMap() function in zod-error-map.ts from the astro codebase.
Entity Profile
Dependency Diagram
graph TD 0518bc8c_7d4c_5826_2e65_b4a7da62d4f9["errorMap()"] c4702bad_5cd3_4ff4_dfb3_9107bd3ef578["zod-error-map.ts"] 0518bc8c_7d4c_5826_2e65_b4a7da62d4f9 -->|defined in| c4702bad_5cd3_4ff4_dfb3_9107bd3ef578 4ed161e9_f116_22b9_deb9_7f42b758780c["flattenErrorPath()"] 0518bc8c_7d4c_5826_2e65_b4a7da62d4f9 -->|calls| 4ed161e9_f116_22b9_deb9_7f42b758780c 8af5bf3f_8e24_dfff_812f_a0fddeb7d74d["prefix()"] 0518bc8c_7d4c_5826_2e65_b4a7da62d4f9 -->|calls| 8af5bf3f_8e24_dfff_812f_a0fddeb7d74d c4289dde_e80c_3ee3_26bf_34ab19c7c1e0["getTypeOrLiteralMsg()"] 0518bc8c_7d4c_5826_2e65_b4a7da62d4f9 -->|calls| c4289dde_e80c_3ee3_26bf_34ab19c7c1e0 ec2e794d_f0eb_1031_50b3_9bed5804ebc8["stringify()"] 0518bc8c_7d4c_5826_2e65_b4a7da62d4f9 -->|calls| ec2e794d_f0eb_1031_50b3_9bed5804ebc8 style 0518bc8c_7d4c_5826_2e65_b4a7da62d4f9 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/astro/src/core/errors/zod-error-map.ts lines 10–102
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(
relativePath ? `${relativePath}: ${_issue.expected}` : _issue.expected,
);
} else if ('values' in _issue) {
expectedShape.push(
..._issue.values.filter((v) => typeof v === 'string').map((v) => `"${v}"`),
);
} else if (relativePath) {
expectedShape.push(relativePath);
}
}
if (expectedShape.length === 1 && !expectedShape[0]?.includes(':')) {
// In this case the expected shape is not an object, but probably a literal type, e.g. `['string']`.
expectedShapes.push(expectedShape.join(''));
} else if (expectedShape.length > 0) {
expectedShapes.push(`{ ${expectedShape.join('; ')} }`);
}
}
if (expectedShapes.length) {
details.push('> Expected type `' + expectedShapes.join(' | ') + '`');
details.push('> Received `' + stringify(issue.input) + '`');
}
}
return {
message: messages.concat(details).join('\n'),
};
} else if (issue.code === 'invalid_type') {
return {
message: prefix(
baseErrorPath,
Domain
Subdomains
Source
Frequently Asked Questions
What does errorMap() do?
errorMap() is a function in the astro codebase, defined in packages/astro/src/core/errors/zod-error-map.ts.
Where is errorMap() defined?
errorMap() is defined in packages/astro/src/core/errors/zod-error-map.ts at line 10.
What does errorMap() call?
errorMap() calls 4 function(s): flattenErrorPath, getTypeOrLiteralMsg, prefix, stringify.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free