Skip to content

feat(zod-mock): add support for readonly fields #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/zod-mock/src/lib/zod-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('zod-mock', () => {
z.object({ discriminator: z.literal('a'), a: z.boolean() }),
z.object({ discriminator: z.literal('b'), b: z.string() }),
]),
readonly: z.boolean().readonly(),
});

const mockData = generateMock(schema);
Expand All @@ -58,6 +59,7 @@ describe('zod-mock', () => {
expect(mockData.set).toBeTruthy();
expect(mockData.map).toBeTruthy();
expect(mockData.discriminatedUnion).toBeTruthy();
expect(typeof mockData.readonly).toEqual('boolean');
});

it('should generate mock data of the appropriate type when the field names overlap Faker properties that are not valid functions', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/zod-mock/src/lib/zod-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,13 @@ function parseLazy(
return generateMock(zodRef._def.getter(), options);
}

function parseReadonly(
zodRef: z.ZodReadonly<ZodTypeAny>,
options?: GenerateMockOptions
) {
return generateMock(zodRef._def.innerType, options);
}

const workerMap = {
ZodObject: parseObject,
ZodRecord: parseRecord,
Expand Down Expand Up @@ -565,6 +572,7 @@ const workerMap = {
ZodBranded: parseBranded,
ZodNull: () => null,
ZodNaN: () => NaN,
ZodReadonly: parseReadonly,
};

type WorkerKeys = keyof typeof workerMap;
Expand Down
Loading