这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@JasirZaeem
Copy link
Contributor

Add support for using the new omitzero field tag introduced in go-1.24 to mark those fields as optional/not-required

https://tip.golang.org/doc/go1.24#encodingjsonpkgencodingjson

When marshaling, a struct field with the new omitzero option in the struct field tag will be omitted if its value is zero.

Changes

  • Added a new check for omitzero to mark field as optional in schema.go
  • Added new tests in to check omitempty and omitzero in schema_test.go
  • Mentioned the new tag in request-validation.md docs

Lmk if there are other files/places that need changing for this change.

@codecov
Copy link

codecov bot commented Feb 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.00%. Comparing base (b02b165) to head (03bcd0e).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #742   +/-   ##
=======================================
  Coverage   93.00%   93.00%           
=======================================
  Files          23       23           
  Lines        5246     5250    +4     
=======================================
+ Hits         4879     4883    +4     
  Misses        315      315           
  Partials       52       52           

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This pull request adds support for the new "omitzero" field tag introduced in go-1.24 to allow fields with a zero value to be omitted during JSON marshaling.

  • Updated schema processing to treat fields tagged with "omitzero" as optional.
  • Added tests for both "omitempty" and "omitzero" tag behavior.
  • Updated documentation to mention the new tag.

Reviewed Changes

File Description
schema.go Integrated "omitzero" flag in required field logic
schema_test.go Added tests to verify behavior for "omitempty" and "omitzero" tags
docs/docs/features/request-validation.md Updated documentation examples to include "omitzero" tag

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thank you @JasirZaeem 👍

@danielgtaylor danielgtaylor merged commit a90a42c into danielgtaylor:main Feb 27, 2025
5 checks passed
@JasirZaeem
Copy link
Contributor Author

@danielgtaylor Thanks for the great library :)

@JasirZaeem JasirZaeem deleted the make-omitzero-optional branch February 27, 2025 19:35
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.

2 participants