Skip to content

fix: preserve fieldValue for deeply nested validation errors - #36

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/deep-nested-field-value
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/deep-nested-field-value

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Dec 1, 2026 •

Copy link
Copy Markdown

For a rule nested two or more levels deep (fields inside fields, or defaultField on an array of objects), the validation error came back with fieldValue: undefined.

Cause: each nested schema is validated against the nested object (schema.validate(data.value, ...)), but addFullField built fullFields as the full path from the root ([...rule.fullFields, key]). complementError then reads getValue(source, rule.fullFields), where source is only the nested object, so a path longer than one key could not be resolved. At depth 1 the path is just [key], which is why that case worked.

The fix makes fullFields the key relative to the object being validated ([key]); fullField (the dotted name used for field and messages) is unchanged.

Before / after for { user: { address: { zip: 123 } } } with zip: [{ type: 'string' }]:

error
before { field: 'user.address.zip', fieldValue: undefined, message: ... }
after { field: 'user.address.zip', fieldValue: 123, message: ... }

Tests: tests/deep.spec.ts gets two cases (nested object, and array of objects via defaultField); both fail on the original and pass now. rc-test passes (16 suites, 118 tests); eslint src has 0 errors (3 warnings, same on the base branch) and tsc --noEmit is clean.

One thing to be aware of: rule.fullFields is also visible to custom validators, and at depth 2+ it is now [key] instead of the full path from the root. I looked in this repo and found no other use. I did not check other projects that depend on this package.

Summary by CodeRabbit

  • 修复
    • 修正深层嵌套字段的验证路径,确保对象和数组中的字段错误信息能够准确显示。
  • 测试
    • 增加对嵌套对象及数组对象字段的验证测试,覆盖错误路径、错误值和错误消息。

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

此更改调整嵌套字段的完整路径构造方式,并新增深层对象及数组对象的验证测试。测试覆盖错误字段、错误值、错误消息,以及对象验证中的 fields 错误映射。

Changes

嵌套字段路径验证

Layer / File(s) Summary
字段路径构造与深层验证测试
src/index.ts, tests/deep.spec.ts
addFullField 现在仅将当前字段名写入 fullFields。新增测试验证 user.address.zip 和 items.0.name 的错误字段、错误值及错误消息;对象测试还检查 fields 错误映射。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: yiminghe

Merge Risk: Minimal · up to 3505d

Validation errors for deeply nested fields now keep their value, and the reported error path and message are unchanged. The tests cover the targeted cases. The only note is that custom validators reading rule.fullFields at depth 2 or more now see a shorter path.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: preserve fieldValue for deeply nested validation errors”描述了 PR 的主要变更:修复深层嵌套校验错误中 fieldValue 丢失的问题。原始摘要显示 src/index.ts 中 addFullField 只写入当前字段名到 fullFields,并新增 tests/deep.spec.ts 中的深层嵌套测试,与标题一致。…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

标题“fix: preserve fieldValue for deeply nested validation errors”描述了 PR 的主要变更:修复深层嵌套校验错误中 fieldValue 丢失的问题。原始摘要显示 src/index.ts 中 addFullField 只写入当前字段名到 fullFields,并新增 tests/deep.spec.ts 中的深层嵌套测试,与标题一致。PR 目标也指出,修改目的是在两层或更深的嵌套规则中保留 fieldValue。

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

小兔看着路径变清楚,
深层字段也能报得准。
对象里的邮编有了名字,
数组中的名字也不再迷路。
验证结果整齐地跳进日志。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 56556ab4-baaf-45ec-921b-e9cbde3a0186
Commits

Reviewing files that changed from the base of the PR and between 8cca839 and 3505d12.

Files selected for processing: 2

  • src/index.ts
  • tests/deep.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.36%. Comparing base (8cca839) to head (3505d12).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #36      +/-   ##
==========================================
- Coverage   82.38%   82.36%   -0.03%     
==========================================
  Files          25       25              
  Lines         653      652       -1     
  Branches      236      237       +1     
==========================================
- Hits          538      537       -1     
  Misses        114      114              
  Partials        1        1              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mokshupadhyay

Copy link
Copy Markdown
Author

Hi @yoyo837, a friendly ping: this is ready for review whenever you have time. Happy to adjust anything. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant