Skip to content

Commit

Permalink
Add invalid_literal issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McDonnell committed Mar 27, 2022
1 parent baf9b53 commit 823dda9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
13 changes: 12 additions & 1 deletion deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { util } from "./helpers/util.ts";

export const ZodIssueCode = util.arrayToEnum([
"invalid_type",
"invalid_literal",
"custom",
"invalid_union",
"invalid_union_discriminator",
Expand All @@ -23,7 +24,6 @@ export type ZodIssueCode = keyof typeof ZodIssueCode;

export type ZodIssueBase = {
path: (string | number)[];
// code: ZodIssueCode;
message?: string;
};

Expand All @@ -33,6 +33,11 @@ export interface ZodInvalidTypeIssue extends ZodIssueBase {
received: ZodParsedType;
}

export interface ZodInvalidLiteralIssue extends ZodIssueBase {
code: typeof ZodIssueCode.invalid_literal;
expected: unknown;
}

export interface ZodUnrecognizedKeysIssue extends ZodIssueBase {
code: typeof ZodIssueCode.unrecognized_keys;
keys: string[];
Expand Down Expand Up @@ -106,6 +111,7 @@ export type DenormalizedError = { [k: string]: DenormalizedError | string[] };

export type ZodIssueOptionalMessage =
| ZodInvalidTypeIssue
| ZodInvalidLiteralIssue
| ZodUnrecognizedKeysIssue
| ZodInvalidUnionIssue
| ZodInvalidUnionDiscriminatorIssue
Expand Down Expand Up @@ -286,6 +292,11 @@ export const defaultErrorMap = (
message = `Expected ${issue.expected}, received ${issue.received}`;
}
break;
case ZodIssueCode.invalid_literal:
message = `Invalid literal value, expected ${JSON.stringify(
issue.expected
)}`;
break;
case ZodIssueCode.unrecognized_keys:
message = `Unrecognized key(s) in object: ${issue.keys
.map((k) => `'${k}'`)
Expand Down
4 changes: 3 additions & 1 deletion deno/lib/__tests__/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ test("literal default error message", () => {
} 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");
expect(zerr.issues[0].message).toEqual(
`Invalid literal value, expected "Tuna"`
);
}
});

Expand Down
5 changes: 2 additions & 3 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2987,9 +2987,8 @@ export class ZodLiteral<T> extends ZodType<T, ZodLiteralDef<T>> {
if (input.data !== this._def.value) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: getParsedType(this._def.value),
received: ctx.parsedType,
code: ZodIssueCode.invalid_literal,
expected: this._def.value,
});
return INVALID;
}
Expand Down
13 changes: 12 additions & 1 deletion src/ZodError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { util } from "./helpers/util";

export const ZodIssueCode = util.arrayToEnum([
"invalid_type",
"invalid_literal",
"custom",
"invalid_union",
"invalid_union_discriminator",
Expand All @@ -23,7 +24,6 @@ export type ZodIssueCode = keyof typeof ZodIssueCode;

export type ZodIssueBase = {
path: (string | number)[];
// code: ZodIssueCode;
message?: string;
};

Expand All @@ -33,6 +33,11 @@ export interface ZodInvalidTypeIssue extends ZodIssueBase {
received: ZodParsedType;
}

export interface ZodInvalidLiteralIssue extends ZodIssueBase {
code: typeof ZodIssueCode.invalid_literal;
expected: unknown;
}

export interface ZodUnrecognizedKeysIssue extends ZodIssueBase {
code: typeof ZodIssueCode.unrecognized_keys;
keys: string[];
Expand Down Expand Up @@ -106,6 +111,7 @@ export type DenormalizedError = { [k: string]: DenormalizedError | string[] };

export type ZodIssueOptionalMessage =
| ZodInvalidTypeIssue
| ZodInvalidLiteralIssue
| ZodUnrecognizedKeysIssue
| ZodInvalidUnionIssue
| ZodInvalidUnionDiscriminatorIssue
Expand Down Expand Up @@ -286,6 +292,11 @@ export const defaultErrorMap = (
message = `Expected ${issue.expected}, received ${issue.received}`;
}
break;
case ZodIssueCode.invalid_literal:
message = `Invalid literal value, expected ${JSON.stringify(
issue.expected
)}`;
break;
case ZodIssueCode.unrecognized_keys:
message = `Unrecognized key(s) in object: ${issue.keys
.map((k) => `'${k}'`)
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ test("literal default error message", () => {
} 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");
expect(zerr.issues[0].message).toEqual(
`Invalid literal value, expected "Tuna"`
);
}
});

Expand Down
5 changes: 2 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2987,9 +2987,8 @@ export class ZodLiteral<T> extends ZodType<T, ZodLiteralDef<T>> {
if (input.data !== this._def.value) {
const ctx = this._getOrReturnCtx(input);
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: getParsedType(this._def.value),
received: ctx.parsedType,
code: ZodIssueCode.invalid_literal,
expected: this._def.value,
});
return INVALID;
}
Expand Down

0 comments on commit 823dda9

Please sign in to comment.