Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: avoid unnecessary error maps #2532

Merged
merged 3 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions deno/lib/helpers/parseUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export const makeIssue = (params: {
path: fullPath,
};

if (issueData.message !== undefined) {
return {
...issueData,
path: fullPath,
message: issueData.message,
};
}

let errorMessage = "";
const maps = errorMaps
.filter((m) => !!m)
Expand All @@ -28,7 +36,7 @@ export const makeIssue = (params: {
return {
...issueData,
path: fullPath,
message: issueData.message ?? errorMessage,
message: errorMessage,
};
};

Expand Down Expand Up @@ -65,15 +73,16 @@ export function addIssueToContext(
ctx: ParseContext,
issueData: IssueData
): void {
const overrideMap = getErrorMap();
const issue = makeIssue({
issueData: issueData,
data: ctx.data,
path: ctx.path,
errorMaps: [
ctx.common.contextualErrorMap, // contextual error map is first priority
ctx.schemaErrorMap, // then schema-bound map if available
getErrorMap(), // then global override map
defaultErrorMap, // then global default map
overrideMap, // then global override map
overrideMap === defaultErrorMap ? undefined : defaultErrorMap, // then global default map
].filter((x) => !!x) as ZodErrorMap[],
});
ctx.common.issues.push(issue);
Expand Down
15 changes: 12 additions & 3 deletions src/helpers/parseUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export const makeIssue = (params: {
path: fullPath,
};

if (issueData.message !== undefined) {
return {
...issueData,
path: fullPath,
message: issueData.message,
};
}

let errorMessage = "";
const maps = errorMaps
.filter((m) => !!m)
Expand All @@ -28,7 +36,7 @@ export const makeIssue = (params: {
return {
...issueData,
path: fullPath,
message: issueData.message ?? errorMessage,
message: errorMessage,
};
};

Expand Down Expand Up @@ -65,15 +73,16 @@ export function addIssueToContext(
ctx: ParseContext,
issueData: IssueData
): void {
const overrideMap = getErrorMap();
const issue = makeIssue({
issueData: issueData,
data: ctx.data,
path: ctx.path,
errorMaps: [
ctx.common.contextualErrorMap, // contextual error map is first priority
ctx.schemaErrorMap, // then schema-bound map if available
getErrorMap(), // then global override map
defaultErrorMap, // then global default map
overrideMap, // then global override map
overrideMap === defaultErrorMap ? undefined : defaultErrorMap, // then global default map
].filter((x) => !!x) as ZodErrorMap[],
});
ctx.common.issues.push(issue);
Expand Down
Loading