Skip to content

Commit

Permalink
Merge pull request #777 from pasieronen/no-enum-value-in-error
Browse files Browse the repository at this point in the history
Don't include received value in enum/literal error messages (fixes #461)
  • Loading branch information
colinhacks authored Dec 30, 2021
2 parents 9ef076d + 5cd8d7b commit a118887
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 10 deletions.
4 changes: 1 addition & 3 deletions deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ export const defaultErrorMap = (
case ZodIssueCode.invalid_enum_value:
message = `Invalid enum value. Expected ${issue.options
.map((val) => (typeof val === "string" ? `'${val}'` : val))
.join(" | ")}, received ${
typeof _ctx.data === "string" ? `'${_ctx.data}'` : _ctx.data
}`;
.join(" | ")}`;
break;
case ZodIssueCode.invalid_arguments:
message = `Invalid function arguments`;
Expand Down
23 changes: 23 additions & 0 deletions deno/lib/__tests__/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,29 @@ test("strict error message", () => {
}
});

test("enum default error message", () => {
try {
z.enum(["Tuna", "Trout"]).parse("Salmon");
} catch (err) {
const zerr: z.ZodError = err as any;
expect(zerr.issues.length).toEqual(1);
expect(zerr.issues[0].message).toEqual(
"Invalid enum value. Expected 'Tuna' | 'Trout'"
);
expect(zerr.issues[0].message).not.toContain("Salmon");
}
});

test("literal default error message", () => {
try {
z.literal("Tuna").parse("Trout");
} catch (err) {
const zerr: z.ZodError = err as any;
expect(zerr.issues.length).toEqual(1);
expect(zerr.issues[0].message).toEqual("Expected string, received string");
}
});

// test("dont short circuit on continuable errors", () => {
// const user = z
// .object({
Expand Down
4 changes: 2 additions & 2 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2727,8 +2727,8 @@ export class ZodLiteral<T extends any> extends ZodType<T, ZodLiteralDef<T>> {
if (ctx.data !== this._def.value) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: this._def.value as any,
received: ctx.data,
expected: getParsedType(this._def.value),
received: ctx.parsedType,
});
return INVALID;
}
Expand Down
4 changes: 1 addition & 3 deletions src/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ export const defaultErrorMap = (
case ZodIssueCode.invalid_enum_value:
message = `Invalid enum value. Expected ${issue.options
.map((val) => (typeof val === "string" ? `'${val}'` : val))
.join(" | ")}, received ${
typeof _ctx.data === "string" ? `'${_ctx.data}'` : _ctx.data
}`;
.join(" | ")}`;
break;
case ZodIssueCode.invalid_arguments:
message = `Invalid function arguments`;
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,29 @@ test("strict error message", () => {
}
});

test("enum default error message", () => {
try {
z.enum(["Tuna", "Trout"]).parse("Salmon");
} catch (err) {
const zerr: z.ZodError = err as any;
expect(zerr.issues.length).toEqual(1);
expect(zerr.issues[0].message).toEqual(
"Invalid enum value. Expected 'Tuna' | 'Trout'"
);
expect(zerr.issues[0].message).not.toContain("Salmon");
}
});

test("literal default error message", () => {
try {
z.literal("Tuna").parse("Trout");
} catch (err) {
const zerr: z.ZodError = err as any;
expect(zerr.issues.length).toEqual(1);
expect(zerr.issues[0].message).toEqual("Expected string, received string");
}
});

// test("dont short circuit on continuable errors", () => {
// const user = z
// .object({
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2727,8 +2727,8 @@ export class ZodLiteral<T extends any> extends ZodType<T, ZodLiteralDef<T>> {
if (ctx.data !== this._def.value) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: this._def.value as any,
received: ctx.data,
expected: getParsedType(this._def.value),
received: ctx.parsedType,
});
return INVALID;
}
Expand Down

0 comments on commit a118887

Please sign in to comment.