Merged
Conversation
Author
|
测试失败似乎是之前提交导致的问题,与此PR无关。 |
Member
好的,我稍后修复下测试 |
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.
Fix #328
问题描述
使用通配符规则(如 Domains.*)验证 slice 中的元素时,如果 slice 是通过 json.Unmarshal 创建的,即使数据完全合法,验证也会错误地失败。
原因分析
validating.go 第 312 行使用 sliceLen < sliceCap 来判断切片是否"元素不足",满足条件时直接跳过逐元素验证,用 nil 调用 required 校验导致失败:
但在 Go 中,cap > len 是完全正常的行为。json.Unmarshal、append 等标准操作在扩容时会分配额外容量:
3 个元素的切片经 JSON 解码后 cap=4,导致 len(3) < cap(4) 为 true,验证器跳过了正常的逐元素检查,直接返回 required 错误。
修复方式
将 sliceLen < sliceCap 改为 sliceLen == 0。这个检查的本意是在 slice 没有任何元素时让 required 验证失败,正确的条件就是 len == 0。
复现方式