Skip to content

Commit

Permalink
perf: avoid unnecessary error maps (#2532)
Browse files Browse the repository at this point in the history
* perf: avoid unnecessary error maps

* Clean up

* Support falsy errorMessage

---------

Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
  • Loading branch information
xuxucode and colinhacks committed Apr 13, 2024
1 parent 6d6ab29 commit 3575727
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
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

0 comments on commit 3575727

Please sign in to comment.