Repository navigation
fix: preserve fieldValue for deeply nested validation errors - #36
mokshupadhyay wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. Walkthrough此更改调整嵌套字段的完整路径构造方式,并新增深层对象及数组对象的验证测试。测试覆盖错误字段、错误值、错误消息,以及对象验证中的 fields 错误映射。 Changes嵌套字段路径验证
Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix Suggested reviewers: Merge Risk:
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Hi @yoyo837, a friendly ping: this is ready for review whenever you have time. Happy to adjust anything. Thanks! |
For a rule nested two or more levels deep (
fieldsinsidefields, ordefaultFieldon an array of objects), the validation error came back withfieldValue: undefined.Cause: each nested schema is validated against the nested object (
schema.validate(data.value, ...)), butaddFullFieldbuiltfullFieldsas the full path from the root ([...rule.fullFields, key]).complementErrorthen readsgetValue(source, rule.fullFields), wheresourceis 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
fullFieldsthe key relative to the object being validated ([key]);fullField(the dotted name used forfieldand messages) is unchanged.Before / after for
{ user: { address: { zip: 123 } } }withzip: [{ type: 'string' }]:{ field: 'user.address.zip', fieldValue: undefined, message: ... }{ field: 'user.address.zip', fieldValue: 123, message: ... }Tests:
tests/deep.spec.tsgets two cases (nested object, and array of objects viadefaultField); both fail on the original and pass now.rc-testpasses (16 suites, 118 tests);eslint srchas 0 errors (3 warnings, same on the base branch) andtsc --noEmitis clean.One thing to be aware of:
rule.fullFieldsis 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