Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Reflector.Requirer to support custom struct field requiring #88

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ type Reflector struct {
// provided by the reflect package.
Namer func(reflect.Type) string

// Requirer custom the requiring of any struct field, its behaves after the
// `json:,omitempty` or `jsonschema:required` parsed.
Requirer func(reflect.StructField, bool) bool

// KeyNamer allows customizing of key names.
// The default is to use the key's name as is, or the json tag if present.
// If a json tag is present, KeyNamer will receive the tag's name as an argument, not the original key name.
Expand Down Expand Up @@ -1005,6 +1009,10 @@ func (r *Reflector) reflectFieldName(f reflect.StructField) (string, bool, bool,
if r.RequiredFromJSONSchemaTags {
required = requiredFromJSONSchemaTags(schemaTags)
}
// Custom struct field requiring.
if r.Requirer != nil {
required = r.Requirer(f, required)
}

nullable := nullableFromJSONSchemaTags(schemaTags)

Expand Down