Skip to content

[zod-mock]: adds support for specifying ZodDiscriminatedUnion variant when discriminator value is set in stringMap options#260

Open
dforesman wants to merge 1 commit intoanatine:mainfrom
dforesman:discriminated-union-specify
Open

[zod-mock]: adds support for specifying ZodDiscriminatedUnion variant when discriminator value is set in stringMap options#260
dforesman wants to merge 1 commit intoanatine:mainfrom
dforesman:discriminated-union-specify

Conversation

@dforesman
Copy link
Copy Markdown

This change updates handling for ZodDiscriminatedUnion to generate a specific variant, if the value for the Discriminator key is defined in stringMap options.

This allows callers to generate mocks that adhere to a specific variant / member of the discriminated union.

Example

Let's say we have a discriminated union, split by the userType key:

const FirstType = z.object({
	userType: z.literal('type-1'),
	userName: z.string(),
});

const SecondType = z.object({
	userType: z.literal('type-2'),
	email: z.string(),
});

const Union = z.discriminatedUnion('userType', [FirstType, SecondType]);

Before

Previously, if you generated a mock from this union, and specified a known value for the userType field, there was no way to guarantee that the mocked object would be of the appropriate type :

const myMockObject = generateMock(Union, {
  stringMap: {
    userType: () => 'type-1'
  }
});
// myMockObject may randomly be either FirstType or SecondType

This is due to the specific variant being chosen randomly by faker's arrayElement helper.

After

With the changes in this PR, discriminator values in stringMap will be used to locate and generate the proper variant:

const myMockFirstTypeObject = generateMock(Union, {
  stringMap: {
    userType: () => 'type-1'
  }
});
// myMockFirstTypeObject is always FirstType

const myMockSecondTypeObject = generateMock(Union, {
  stringMap: {
    userType: () => 'type-2'
  }
});
// myMockSecondTypeObject is always SecondType

If the requested discriminator value is not found in the schema, generateMock will fall back to its existing behavior of randomly selecting a variant:

const myMockNonexistentTypeObject = generateMock(Union, {
  stringMap: {
    userType: () => 'type-3'
  }
});
// myMockNonexistentTypeObject falls back to randomly selecting a variant

const myMockUnspecifiedTypeObject = generateMock(Union);
// myMockUnspecifiedTypeObject randomly chooses a variant (existing behavior)

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Jul 30, 2025

View your CI Pipeline Execution ↗ for commit 33be0bb

Command Status Duration Result
nx affected:test --base=origin/main --codeCoverage ✅ Succeeded 21s View ↗
nx affected:lint --base=origin/main ✅ Succeeded 3s View ↗

☁️ Nx Cloud last updated this comment at 2025-07-30 23:19:19 UTC

@dforesman dforesman marked this pull request as ready for review July 30, 2025 23:19
@dforesman dforesman changed the title feat(zod-mock): adds support for specifying ZodDiscriminatedUnion variant via stringMap options feat(zod-mock): adds support for specifying ZodDiscriminatedUnion variant when discriminator value is set in stringMap options Aug 11, 2025
@dforesman dforesman changed the title feat(zod-mock): adds support for specifying ZodDiscriminatedUnion variant when discriminator value is set in stringMap options [zod-mock]: adds support for specifying ZodDiscriminatedUnion variant when discriminator value is set in stringMap options Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant