Skip to content

Commit

Permalink
Add error parameters to createZodEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McDonnell committed May 22, 2022
1 parent 4459e75 commit 4284011
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ The following steps will get you setup to contribute changes to this repo:

- runs all Jest tests and generates coverage badge

**`yarn test:one`**
**`yarn test enum`**

- runs a single test file
- example: `yarn test src/__tests__/testFileNameHere.ts`
- runs a single test file (e.g. `enum.test.ts`)

**`yarn play`**

Expand Down
3 changes: 3 additions & 0 deletions deno/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ z.string().uuid();
z.string().cuid();
z.string().regex(regex);

// trim whitespace
z.string().trim();

// deprecated, equivalent to .min(1)
z.string().nonempty();

Expand Down
10 changes: 10 additions & 0 deletions deno/lib/__tests__/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ test("readonly enum", () => {
arg.parse("201");
expect(() => arg.parse("202")).toThrow();
});

test("error params", () => {
const result = z
.enum(["test"], { required_error: "REQUIRED" })
.safeParse(undefined);
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues[0].message).toEqual("REQUIRED");
}
});
9 changes: 6 additions & 3 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3021,15 +3021,18 @@ export interface ZodEnumDef<T extends EnumValues = EnumValues>
type Writeable<T> = { -readonly [P in keyof T]: T[P] };

function createZodEnum<U extends string, T extends Readonly<[U, ...U[]]>>(
values: T
values: T,
params?: RawCreateParams
): ZodEnum<Writeable<T>>;
function createZodEnum<U extends string, T extends [U, ...U[]]>(
values: T
values: T,
params?: RawCreateParams
): ZodEnum<T>;
function createZodEnum(values: any) {
function createZodEnum(values: any, params?: RawCreateParams) {
return new ZodEnum({
values: values as any,
typeName: ZodFirstPartyTypeKind.ZodEnum,
...processCreateParams(params),
}) as any;
}

Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ test("readonly enum", () => {
arg.parse("201");
expect(() => arg.parse("202")).toThrow();
});

test("error params", () => {
const result = z
.enum(["test"], { required_error: "REQUIRED" })
.safeParse(undefined);
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues[0].message).toEqual("REQUIRED");
}
});
9 changes: 6 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3021,15 +3021,18 @@ export interface ZodEnumDef<T extends EnumValues = EnumValues>
type Writeable<T> = { -readonly [P in keyof T]: T[P] };

function createZodEnum<U extends string, T extends Readonly<[U, ...U[]]>>(
values: T
values: T,
params?: RawCreateParams
): ZodEnum<Writeable<T>>;
function createZodEnum<U extends string, T extends [U, ...U[]]>(
values: T
values: T,
params?: RawCreateParams
): ZodEnum<T>;
function createZodEnum(values: any) {
function createZodEnum(values: any, params?: RawCreateParams) {
return new ZodEnum({
values: values as any,
typeName: ZodFirstPartyTypeKind.ZodEnum,
...processCreateParams(params),
}) as any;
}

Expand Down

0 comments on commit 4284011

Please sign in to comment.