Skip to content

Commit 166a6a4

Browse files
committed
inline error header
1 parent 050abc9 commit 166a6a4

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

packages/zod/src/config/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function registerConfig<
8888
);
8989
if (!parsedConfig.success)
9090
throw new TypeError(
91-
typifyError(configSchema, parsedConfig.error, "config", namespace, envKeys),
91+
`Invalid config for "${namespace}":\n${typifyError(configSchema, parsedConfig.error, namespace, envKeys)}`,
9292
{ cause: parsedConfig.error },
9393
);
9494
const config = parsedConfig.data;

packages/zod/src/internal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
camelCase,
55
flatMap,
66
get,
7+
isEmpty,
78
isObjectLike,
89
isString,
910
isUndefined,
@@ -198,7 +199,6 @@ export function decodeVariables(
198199
export const typifyError = <S extends $ZodType>(
199200
schema: S,
200201
error: $ZodError,
201-
context: "options" | "config",
202202
namespace: string,
203203
envKeys: Map<string, string> = new Map(),
204204
jsonSchemaOptions: Parameters<typeof toJSONSchema>[1] = { unrepresentable: "any" },
@@ -222,13 +222,13 @@ export const typifyError = <S extends $ZodType>(
222222

223223
// use the env key or the path (prefixed with the namespace to have in the error formatting the namespace name too)
224224
// (e.g. instead of -> at port: ... you get -> at app.port: ... so you don't have to see the header for the namespace name)
225-
const issuePath = [namespace.toLowerCase(), ...issue.path];
225+
const issuePath = isEmpty(namespace) ? issue.path : [namespace.toLowerCase(), ...issue.path];
226226
const path = toDotPath(issuePath);
227227
const via = envKeys.get(path) ? ` via ${envKeys.get(path)}` : "";
228228

229229
lines.push(` → at ${path}${via}${description}`);
230230
}
231231
}
232232

233-
return `Invalid ${context} for "${namespace}":\n${lines.join("\n")}`;
233+
return lines.join("\n");
234234
};

packages/zod/src/module/module.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,12 @@ export class ZodConfigurableModuleBuilder<
199199
return await parseAsync(self.schema, finalOptions);
200200
} catch (err) {
201201
throw err instanceof $ZodError
202-
? new TypeError(typifyError(self.schema, err, "options", this.name), {
203-
cause: err,
204-
})
202+
? new TypeError(
203+
`Invalid options for "${this.name}":\n${typifyError(self.schema, err, this.name)}`,
204+
{
205+
cause: err,
206+
},
207+
)
205208
: err;
206209
}
207210
},
@@ -301,9 +304,12 @@ export class ZodConfigurableModuleBuilder<
301304
return await parseAsync(self.schema, finalOptions);
302305
} catch (err) {
303306
throw err instanceof $ZodError
304-
? new TypeError(typifyError(self.schema, err, "options", this.name), {
305-
cause: err,
306-
})
307+
? new TypeError(
308+
`Invalid options for "${this.name}":\n${typifyError(self.schema, err, this.name)}`,
309+
{
310+
cause: err,
311+
},
312+
)
307313
: err;
308314
}
309315
},
@@ -325,7 +331,10 @@ export class ZodConfigurableModuleBuilder<
325331
return await parseAsync(self.schema, finalOptions);
326332
} catch (err) {
327333
throw err instanceof $ZodError
328-
? new TypeError(typifyError(self.schema, err, "options", this.name), { cause: err })
334+
? new TypeError(
335+
`Invalid options for "${this.name}":\n${typifyError(self.schema, err, this.name)}`,
336+
{ cause: err },
337+
)
329338
: err;
330339
}
331340
},

0 commit comments

Comments
 (0)