-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add .extend
to Zod enums
#3999
base: main
Are you sure you want to change the base?
Add .extend
to Zod enums
#3999
Conversation
WalkthroughThis update introduces a new Changes
Sequence Diagram(s)sequenceDiagram
participant U as User
participant Z as ZodEnum
participant N as ExtendedEnum
U->>Z: Call .extend(newValues)
Z->>Z: Merge existing values with newValues
Z-->>U: Return new ExtendedEnum with combined values
Poem
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ Deploy Preview for guileless-rolypoly-866f8a ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/__tests__/enum.test.ts (1)
44-53
: LGTM! Consider adding more test cases for comprehensive coverage.The test correctly verifies the type inference for the extended enum. However, to ensure robust functionality, consider adding the following test cases:
test("extends - edge cases", () => { const BaseEnum = z.enum(["A", "B"]); // Test extending with empty array const EmptyExtended = BaseEnum.extends([]); util.assertEqual<z.infer<typeof EmptyExtended>, "A" | "B">(true); // Test extending with duplicate values const DuplicateExtended = BaseEnum.extends(["B", "C"]); util.assertEqual<z.infer<typeof DuplicateExtended>, "A" | "B" | "C">(true); // Test runtime behavior expect(DuplicateExtended.options).toEqual(["A", "B", "C"]); expect(() => DuplicateExtended.parse("D")).toThrow(); // Test error mapping const ErrorMappedEnum = BaseEnum.extends(["C"], { errorMap: () => ({ message: "Invalid value!" }) }); const result = ErrorMappedEnum.safeParse("D"); expect(result.success).toBe(false); if (!result.success) { expect(result.error.issues[0].message).toBe("Invalid value!"); } });src/types.ts (1)
4410-4418
: Add JSDoc documentation for the new extends method.The implementation looks good but is missing documentation to explain its purpose and parameters.
Add JSDoc comments like this:
+ /** + * Creates a new enum by extending the current enum with additional values. + * @param values - Array of new values to add to the enum + * @param newDef - Optional parameters to customize the new enum definition + * @returns A new ZodEnum instance containing all values + */ extends<const ToExtend extends readonly [string, ...string[]]>( values: ToExtend, newDef: RawCreateParams = this._def ): ZodEnum<[...T, ...Writeable<ToExtend>]> {README.md (1)
1170-1170
: Markdown Styling: Emphasis MarkersThere are extra spaces inside the emphasis markers on the
.extends()
header. For a cleaner markdown style (per MD037), consider changing
**
.extends()**
to
**
.extends()**
.🧰 Tools
🪛 markdownlint-cli2 (0.17.2)
1170-1170: Spaces inside emphasis markers
null(MD037, no-space-in-emphasis)
1170-1170: Spaces inside emphasis markers
null(MD037, no-space-in-emphasis)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
README.md
(1 hunks)src/__tests__/enum.test.ts
(1 hunks)src/types.ts
(1 hunks)
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
README.md
1170-1170: Spaces inside emphasis markers
null
(MD037, no-space-in-emphasis)
1170-1170: Spaces inside emphasis markers
null
(MD037, no-space-in-emphasis)
🔇 Additional comments (2)
src/types.ts (1)
4410-4418
: LGTM! The implementation is clean and type-safe.The implementation correctly extends the enum by:
- Using proper type constraints
- Efficiently combining arrays
- Preserving the original definition
- Allowing customization via optional params
README.md (1)
1170-1178
: Documentation Addition: Introducing the.extends()
MethodThe new documentation clearly explains how to extend an existing enum with additional values. The example:
const SalmonAndTrout = z.enum(["Salmon", "Trout"]); const FishEnum = SalmonAndTrout.extends(["Tuna"]);illustrates how users can create a superset enum. Please double-check that this example aligns precisely with the implementation in the type definitions (e.g., in
src/types.ts
) for consistency.🧰 Tools
🪛 markdownlint-cli2 (0.17.2)
1170-1170: Spaces inside emphasis markers
null(MD037, no-space-in-emphasis)
1170-1170: Spaces inside emphasis markers
null(MD037, no-space-in-emphasis)
- Add `.extend` method to Enum class in Zod to allow extending enums with additional values
65e0427
to
3dd4a28
Compare
Sorry! Just renamed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/types.ts (1)
4410-4418
: Add documentation for the new.extend()
method.The implementation looks good but would benefit from JSDoc documentation explaining the purpose, parameters and return value.
Add documentation above the method:
+ /** + * Creates a new enum by extending an existing enum with additional values + * @param values - Array of new values to add to the enum + * @param newDef - Optional parameters to override in the new enum + * @returns A new ZodEnum instance containing all values + */ extend<const ToExtend extends readonly [string, ...string[]]>( values: ToExtend, newDef: RawCreateParams = this._def ): ZodEnum<[...T, ...Writeable<ToExtend>]> {README.md (2)
1170-1171
: Formatting Issue: Remove extra spaces in emphasis markers
There are extra spaces inside the emphasis markers in the new heading. To comply with markdown lint MD037 (no-space-in-emphasis), consider changing:-** `.extend()` ** +**`.extend()`**🧰 Tools
🪛 markdownlint-cli2 (0.17.2)
1170-1170: Spaces inside emphasis markers
null(MD037, no-space-in-emphasis)
1170-1170: Spaces inside emphasis markers
null(MD037, no-space-in-emphasis)
1176-1177
: Documentation Continuity
The transition from the.extend()
example to the subsequent section on.exclude/.extract()
is smooth. For clarity, consider adding a brief note that explains when one might use.extend()
versus.exclude
/.extract
so that users can quickly decide the appropriate method for their use case.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
README.md
(1 hunks)src/__tests__/enum.test.ts
(1 hunks)src/types.ts
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/tests/enum.test.ts
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
README.md
1170-1170: Spaces inside emphasis markers
null
(MD037, no-space-in-emphasis)
1170-1170: Spaces inside emphasis markers
null
(MD037, no-space-in-emphasis)
🔇 Additional comments (2)
src/types.ts (1)
4410-4418
: LGTM! Implementation follows best practices.The implementation:
- Maintains immutability by creating a new instance
- Uses const generics for type safety
- Preserves original definition parameters
- Correctly merges the enum values
README.md (1)
1171-1175
: New Enum Extension Example
The new example clearly demonstrates how to extend an existing Zod enum using the newly introduced.extend()
method. The code snippet—creating aSalmonAndTrout
enum and then extending it with"Tuna"
—is self-explanatory and aligns with the intended functionality described in the PR objectives.
.extend
method to Enum class in Zod to allow extending enums with additional valuesEssentially, adds functionality equivalent to the following.
I think it would be useful to have alongside
.extract
and.exclude
. The behavior is also equivalent toz.object
's.extend
. I couldn't find any issues on this or similar, hence the PR. Let me know what you think.Summary by CodeRabbit
New Features
Tests