go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.07k stars 1.29k forks source link

feat: omit anonymous embedded struct #1211

Open abemedia opened 5 months ago

abemedia commented 5 months ago

Fixes Or Enhances

This PR adds an option to omit the names of anonymous embedded structs.

For example right now the code below would produce a namespace of Foo.Bar.Baz, with this code the option WithOmitAnonymousName would instead display Foo.Baz.

type Foo struct {
  Bar
}

type Bar struct {
  Baz string `validate:"required"`
}

I'm not that familiar with the inner workings of validator so apologies if this is implemented in a terrible way. Happy to change it if the concept is acceptable and I get some pointers.

Make sure that you've checked the boxes below before you submit PR:

@go-playground/validator-maintainers

coveralls commented 5 months ago

Coverage Status

coverage: 73.896% (-0.007%) from 73.903% when pulling 80db7adbfa709bff4e2a37d8c757a8e607f3dd1d on abemedia:feat/omit-anonymous-embedded-struct into 84254aeb5a59e615ec0b66ab53b988bc0677f55e on go-playground:master.

abemedia commented 4 months ago

@deankarn any chance of getting this in? If the approach using an option is not preferable I'm also happy to implement an alternative approach, for example having an inline tag the way the stdlib does for JSON.