Repository navigation
New zod@4 defaultObjectBySchema - #93
Conversation
fix: remove JSON Schema conversion for zod@4 `defaultObjectBySchema`
WalkthroughThe update enhances Zod v4 schema support by adding new type guard and helper functions, enabling direct introspection and default value generation for Zod v4 schemas. The logic for handling Zod v3 remains largely unchanged, while Zod v4 processing now avoids JSON schema conversion, relying on recursive schema inspection and new helper utilities. Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant Utils
participant ZodV4Schema
Caller->>Utils: defaultObjectBySchema(zodV4Schema, original)
Utils->>ZodV4Schema: Inspect shape, catchall, and properties
Utils->>Utils: Recursively process fields (optional, default, nullable, array, record)
Utils->>Caller: Return constructed default object
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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 (
|
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/utils.ts (3)
90-90: Fix typo in comment-// replace of effercts +// replacement for effects
157-177: Consider refactoring to eliminate code duplicationThe pipe unwrapping logic is duplicated from
getZod4SchemaInnerType. Consider refactoring to reuse that function.export const isZod4SchemaOptional = <Type extends z4.$ZodType>( schema: | Type | z4.$ZodPipe<Type> | z4.$ZodPipe<any, Type> | z4.$ZodOptional<Type>, ) => { - let toReturn = schema - while (isZod4Pipe(toReturn)) { - if (isZod4Transform(toReturn._zod.def.out)) { - toReturn = toReturn._zod.def.in - } - else { - toReturn = toReturn._zod.def.out as Type - } - } - if (isZod4Optional(toReturn)) { - return true - } - return false + const innerType = getZod4SchemaInnerType(schema) + return isZod4Optional(innerType) }
321-324: Add type assertion for record value accessWhen accessing
originalValue[recordKey], TypeScript may not infer the correct type.return [key, Object.keys(originalValue).reduce((acc: Record<string, unknown>, recordKey: string) => { - acc[recordKey] = defaultObjectBySchema(valueType, originalValue[recordKey]) + acc[recordKey] = defaultObjectBySchema(valueType, (originalValue as Record<string, unknown>)[recordKey]) return acc }, {})]
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/utils.ts(6 hunks)
🔇 Additional comments (6)
src/utils.ts (6)
14-36: Helper functions are well-implementedThe internal helper functions
_getValueTypeand_isValueCompatibleWithSchemacorrectly handle type determination and compatibility checking, including the special case for integer values in JSON Schema.
42-98: Zod v4 type guards are correctly implementedThe new type guards properly check the internal structure of Zod v4 types using
_zod.def.type, mirroring the pattern used for Zod v3 guards.
117-137: Inner type extraction logic is soundThe
getZod4SchemaInnerTypefunction correctly unwraps nested pipes, transforms, and optionals, with appropriate handling for transform types that returns the input schema rather than the transformed output.
185-185: Good refactoring to use helper functionUsing the
_isValueCompatibleWithSchemahelper improves code maintainability by centralizing the compatibility logic.
266-266: Verify the unknown keys logicThe current logic appears inverted. Unknown keys should typically be preserved when a catchall is defined (and not of type 'never'). The double negation makes this confusing.
Consider clarifying this logic:
-const unknownKeys = !(!innerType._zod.def.catchall || innerType._zod.def.catchall?._zod.def.type === 'never') +const unknownKeys = innerType._zod.def.catchall && innerType._zod.def.catchall._zod.def.type !== 'never'
262-343: Zod v4 implementation successfully removes JSON Schema dependencyThe direct schema introspection approach is well-implemented and maintains feature parity with the Zod v3 handling. The implementation correctly handles all schema types including defaults, nullable, optional, arrays, records, and nested objects.



fix: remove JSON Schema conversion for zod@4
defaultObjectBySchemaSummary by CodeRabbit
New Features
Refactor