-
Notifications
You must be signed in to change notification settings - Fork 7.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
fix: repair the unexpected form default value #5567
base: main
Are you sure you want to change the base?
Conversation
|
WalkthroughThe pull request updates the form context functionality by enhancing schema validation and initialization. The changes expand imported Zod types, modify the logic in the Changes
Sequence Diagram(s)sequenceDiagram
participant GF as generateInitialValues
participant CD as getCustomDefaultValue
participant ZF as Zod Validation Schema
GF->>ZF: Parse form rules for each field
GF->>CD: Check if custom default exists for rule
CD-->>GF: Return custom default (if exists)
GF->>ZF: Merge or apply default value based on type
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 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 (
|
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: 1
🧹 Nitpick comments (1)
packages/@core/ui-kit/form-ui/src/use-form-context.ts (1)
68-99
: Handle potential type mismatch and other Zod types
The recursive default extraction is effective forZodString
,ZodNumber
,ZodObject
, andZodIntersection
. However, returningnull
forZodNumber
might cause type issues if the form logic expects a numeric value. Also consider how you'd like to handle boolean or array types (e.g.,ZodBoolean
,ZodArray
) to avoid unexpected runtime errors:
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/@core/ui-kit/form-ui/src/use-form-context.ts
(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: post-update (windows-latest)
- GitHub Check: post-update (ubuntu-latest)
🔇 Additional comments (1)
packages/@core/ui-kit/form-ui/src/use-form-context.ts (1)
13-13
: Imports for advanced schema usage
Nice addition ofZodIntersection
,ZodNumber
,ZodObject
, andZodString
fromzod
. These imports enable more flexible validation and facilitate the new custom default extraction logic.
// 检查规则是否适合提取默认值 | ||
const customDefaultValue = getCustomDefaultValue(item.rules); | ||
if (customDefaultValue === undefined) { | ||
// 否则将规则添加到 zodObject | ||
zodObject[item.fieldName] = item.rules; | ||
} else { | ||
// 如果有自定义默认值,直接使用 | ||
initialValues[item.fieldName] = customDefaultValue; | ||
} |
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.
🛠️ Refactor suggestion
Retain validation rules to avoid skipping schema checks
Currently, adding the rule to zodObject
only happens if no custom default is found. Fields with custom defaults never make it into zodObject
, potentially skipping validation. If you want to both apply the custom default and preserve validation, consider merging the rule into the shape regardless:
- if (customDefaultValue === undefined) {
- zodObject[item.fieldName] = item.rules;
- } else {
- initialValues[item.fieldName] = customDefaultValue;
- }
+ zodObject[item.fieldName] = item.rules;
+ if (customDefaultValue !== undefined) {
+ initialValues[item.fieldName] = customDefaultValue;
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// 检查规则是否适合提取默认值 | |
const customDefaultValue = getCustomDefaultValue(item.rules); | |
if (customDefaultValue === undefined) { | |
// 否则将规则添加到 zodObject | |
zodObject[item.fieldName] = item.rules; | |
} else { | |
// 如果有自定义默认值,直接使用 | |
initialValues[item.fieldName] = customDefaultValue; | |
} | |
// 检查规则是否适合提取默认值 | |
const customDefaultValue = getCustomDefaultValue(item.rules); | |
zodObject[item.fieldName] = item.rules; | |
if (customDefaultValue !== undefined) { | |
initialValues[item.fieldName] = customDefaultValue; | |
} |
Description
当表单项校验规则为number\intersection等时,会出现不符合预期的默认值,如issue #5451 所述。
本提交增加了getCustomDefaultValue方法,以处理此类情况。
Checklist
pnpm run docs:dev
command.pnpm test
.feat:
,fix:
,perf:
,docs:
, orchore:
.Summary by CodeRabbit