feat(zod-mock): pass path context to stringMap callbacks - #271
Open
matt-halliday wants to merge 3 commits into
Open
feat(zod-mock): pass path context to stringMap callbacks#271matt-halliday wants to merge 3 commits into
stringMap callbacks#271matt-halliday wants to merge 3 commits into
Conversation
|
View your CI Pipeline Execution ↗ for commit e4d945c
💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗ ☁️ Nx Cloud last updated this comment at |
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.
Problem
stringMapcallbacks have no way to distinguish where in the schema a field was located. If the same field name (e.g.name) appeared under different parent objects, every instance received the same generated value, making it impossible to, say, produce an institution name vs. a person's name from a singlenameentry.Limitation also highlighted in these issues, though this is arguably a more discrete solution than deep object
stringMapsupport, or docblock/annotation parsing, and provides support for arrays and tuples if needed:#141
#134
Proposed Solution
stringMapcallbacks now receive actxargument with apath: (string | number)[]property, following the same convention used by Zod's own error objects, representing the full path to the current field.Changes
zod-mock.ts:parseObjectnow accumulates_pathon options as it recurses,parseStringpasses{ path }when invoking astringMapgenerator.GenerateMockOptions:stringMaptype updated from(...args: any[]) => stringtoctx: StringMapContext) => string._pathadded as an@internalfield.StringMapContext: new exported interfaceBackwards compatibility
Existing callbacks continue to work, the extra argument is silently ignored. All existing tests pass. One new test added.
The context is also an object so additions in future might be added without impacting any new consumers of this
pathimplementation.