[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
Conversation
…ariant via `stringMap` options
|
View your CI Pipeline Execution ↗ for commit 33be0bb
☁️ Nx Cloud last updated this comment at |
ZodDiscriminatedUnion variant via stringMap optionsZodDiscriminatedUnion variant when discriminator value is set in stringMap options
ZodDiscriminatedUnion variant when discriminator value is set in stringMap optionsZodDiscriminatedUnion variant when discriminator value is set in stringMap options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change updates handling for
ZodDiscriminatedUnionto generate a specific variant, if the value for theDiscriminatorkey is defined instringMapoptions.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
userTypekey:Before
Previously, if you generated a mock from this union, and specified a known value for the
userTypefield, there was no way to guarantee that the mocked object would be of the appropriate type :This is due to the specific variant being chosen randomly by
faker'sarrayElementhelper.After
With the changes in this PR, discriminator values in
stringMapwill be used to locate and generate the proper variant:If the requested discriminator value is not found in the schema,
generateMockwill fall back to its existing behavior of randomly selecting a variant: