Merged
Conversation
# Conflicts: # .gitignore # validator.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
当前情况分析
go-playground/validator 是一个广泛使用的 Go 数据验证库。目前的 FieldError 接口提供了字段级别的错误信息,但没有直接提供访问父级结构的机制。
PR 提案技术评估
您的提案是添加 TopFieldError 接口,扩展 FieldError 添加 Top() 方法来返回校验字段对应的父级反射值。这将:
保持向后兼容性(不会破坏现有实现)
// TopFieldError 扩展接口 type TopFieldError interface { FieldError // Top 返回当前校验字段的父级反射值 Top() reflect.Value } 实现要点: 应该在 validator/v10/errors.go 中添加新接口定义 在现有的 fieldError 结构中添加父级反射值的存储字段 修改验证过程中填充父级反射值的逻辑 添加相应的测试用例 4. 用例场景 这个改动将支持如下使用场景: type User struct { Name string `validate:"required"` Address struct { Street string `json:"street" validate:"required"` } } err := validate.Struct(user) if err != nil { for _, fieldErr := range err.(validator.ValidationErrors) { if topErr, ok := fieldErr.(validator.TopFieldError); ok { parent := topErr.Top() // 现在可以获取父结构的元信息 fmt.Println("Parent type:", parent.Type()) } } } 5. 需要考虑的问题 性能影响:存储父级反射值会增加少量内存开销 兼容性:需要确保不破坏现有功能 文档更新:需要更新库文档说明新功能 测试覆盖率:需要为新增功能添加充分测试 6. PR 提交建议 先创建一个 issue 讨论这个功能提案 保持 PR 范围专注(仅实现 TopFieldError 接口) 提供清晰的文档说明和用例 确保所有测试通过 遵循项目的贡献指南为更高级的校验功能(如访问父结构标签)铺路
提升库的灵活性
3. 实现建议
建议采用以下实现方案: