Skip to content

Restore length check for required on empty array/map/slice - #287

Merged
cinar merged 2 commits into
mainfrom
fix-285-required-empty-collection
Sep 7, 2026
Merged

Restore length check for required on empty array/map/slice#287
cinar merged 2 commits into
mainfrom
fix-285-required-empty-collection

Conversation

@cinar

@cinar cinar commented Sep 7, 2026

Copy link
Copy Markdown
Owner

Summary

  • reflectRequired in v2 was purely IsZero()-based; per the stdlib's own documented semantics, IsZero() on a slice only reports true for a nil slice, so a non-nil, zero-length slice/map (e.g. an explicit []string{} or emails: [] in YAML) silently passed required/@required -- a regression from v1, which had a dedicated length check for Array/Map/Slice kinds
  • Restored that length-based branch so required on a list/map-shaped field means "must have at least one item," matching v1's behavior
  • Added tests for empty/non-empty slice and map cases

Fixes #285

Test plan

  • go test -cover ./... — 100% coverage maintained
  • task lint (gosec) — 0 issues
  • Verified repro from the issue: Required([]string{}) now returns ErrRequired; Required([]string{"x"}) does not

🤖 Generated with Claude Code

https://claude.ai/code/session_01CgPf2eG8h9xHh7kHFJ4eCs

reflectRequired was purely IsZero()-based, but reflect.Value.IsZero()
on a slice only reports true for a nil slice -- a non-nil, zero-length
slice/map (e.g. an explicit []string{} or emails: [] in YAML) passed
required/@required silently. v1 had a dedicated length check for
Array/Map/Slice kinds; restore that behavior so an empty collection is
rejected the same way a nil one is.

Fixes #285

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CgPf2eG8h9xHh7kHFJ4eCs
@codecov

codecov Bot commented Sep 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (685109c) to head (4c3aa4c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #287   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           86        86           
  Lines         1807      1812    +5     
=========================================
+ Hits          1807      1812    +5     

☔ 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.

@cinar
cinar merged commit 9e6d7e3 into main Sep 7, 2026
12 checks passed
@cinar
cinar deleted the fix-285-required-empty-collection branch September 7, 2026 04:26
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.

v2: required/@required no longer catches an explicitly empty slice/map (regression from v1)

1 participant