fix(zod): properly infer scalar array types#2500
fix(zod): properly infer scalar array types#2500haltcase wants to merge 5 commits intozenstackhq:devfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughUpdated internal Zod type composition to ensure scalar array fields are represented as Zod arrays; added a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
Required to support list types Regenerated schema with: `pnpm --package=@zenstackhq/cli dlx zen generate --schema ./test/schema/schema.zmodel --output ./test/schema --generate-models=false --generate-input=false`
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/zod/src/types.ts (1)
53-76:⚠️ Potential issue | 🟡 MinorBoth
GetModelCreateFieldsShapeandGetModelUpdateFieldsShapeneed theZodArrayIfwrapper for consistent array type inference.These types were missing the same fix applied to
GetModelFieldsShape. While runtime validation works correctly (the factory'sapplyCardinalitymethod properly wraps schemas with.array()), the TYPE definitions don't reflect this, causing incorrect TypeScript type inference for scalar array fields in create/update operations.Apply the suggested fixes to ensure proper type inference across all shape types.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/zod/src/types.ts` around lines 53 - 76, Wrap the mapped field Zod schema with ZodArrayIf in both GetModelCreateFieldsShape and GetModelUpdateFieldsShape like you did in GetModelFieldsShape: replace MapModelFieldToZod<Schema, Model, Field> with ZodArrayIf<MapModelFieldToZod<Schema, Model, Field>, /* same array-condition used in GetModelFieldsShape */> so the existing wrappers (ZodOptionalIf, ZodOptionalAndNullableIf, z.ZodOptional) continue to apply to the array-aware schema; update both GetModelCreateFieldsShape and GetModelUpdateFieldsShape declarations accordingly, keeping the other conditional filters (FieldIsRelation, FieldIsComputed, FieldIsDelegateDiscriminator) and utilities (ModelFieldIsOptional, FieldHasDefault) intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@packages/zod/src/types.ts`:
- Around line 53-76: Wrap the mapped field Zod schema with ZodArrayIf in both
GetModelCreateFieldsShape and GetModelUpdateFieldsShape like you did in
GetModelFieldsShape: replace MapModelFieldToZod<Schema, Model, Field> with
ZodArrayIf<MapModelFieldToZod<Schema, Model, Field>, /* same array-condition
used in GetModelFieldsShape */> so the existing wrappers (ZodOptionalIf,
ZodOptionalAndNullableIf, z.ZodOptional) continue to apply to the array-aware
schema; update both GetModelCreateFieldsShape and GetModelUpdateFieldsShape
declarations accordingly, keeping the other conditional filters
(FieldIsRelation, FieldIsComputed, FieldIsDelegateDiscriminator) and utilities
(ModelFieldIsOptional, FieldHasDefault) intact.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: b733e876-e054-4103-845d-a68d36aa91bc
📒 Files selected for processing (1)
packages/zod/src/types.ts
Closes #2499
I haven't added a test case yetbecause the current zmodel used for testing the zod package specifiessqlitewhich doesn't support array types.Edit: I've now added a test case but it required switching the provider to
postgresqland regenerating the schema. Let me know if that's not desired.Summary by CodeRabbit
Refactor
Tests