Skip to content

Commit

Permalink
feat: extend native enum to support const assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucakovic-godaddy committed Mar 13, 2023
1 parent b2cdc8f commit c70336a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 18 additions & 0 deletions packages/zod-mock/src/lib/zod-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,24 @@ describe('zod-mock', () => {
expect(typeof value).toBe('string');
});

it('ZodNativeEnum', () => {
enum NativeEnum {
a = 1,
b = 2,
}

const first = generateMock(z.nativeEnum(NativeEnum));
expect(first === 1 || first === 2);

const ConstAssertionEnum = {
a: 1,
b: 2
} as const;

const second = generateMock(z.nativeEnum(ConstAssertionEnum));
expect(second === 1 || second === 2);
});

it('ZodFunction', () => {
const func = generateMock(z.function(z.tuple([]), z.string()));
expect(func).toBeTruthy();
Expand Down
3 changes: 1 addition & 2 deletions packages/zod-mock/src/lib/zod-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ function parseNativeEnum(
) {
const fakerInstance = options?.faker || faker;
const { values } = zodRef._def;
const value = fakerInstance.helpers.arrayElement(values);
return values[value];
return fakerInstance.helpers.objectValue(values);
}

function parseLiteral(zodRef: z.ZodLiteral<any>) {
Expand Down

0 comments on commit c70336a

Please sign in to comment.