Skip to content

Feat: add tag to skip namespace in embedded structures#1426

Open
arxeiss wants to merge 1 commit into
go-playground:masterfrom
arxeiss:master
Open

Feat: add tag to skip namespace in embedded structures#1426
arxeiss wants to merge 1 commit into
go-playground:masterfrom
arxeiss:master

Conversation

@arxeiss

@arxeiss arxeiss commented May 7, 2025

Copy link
Copy Markdown

Enhances

Introducing new skipns validate tag, that allows to skip the name of embedded struct, when printing invalid field namspace.

type Outer struct {
	Embedded `validate:"skipns"`
}

See #1413 for more extensive examples and discussion.

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

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

Fixing #1413 and #776

@arxeiss arxeiss requested a review from a team as a code owner May 7, 2025 13:51
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 73.672% (+0.02%) from 73.655%
when pulling f881394 on arxeiss:master
into 20f7df6 on go-playground:master.

@ns-vchen

Copy link
Copy Markdown

@arxeiss thanks for having this PR, I'm expecting the merge
BTW, I notice that you also need to change the code here.

it is widely used by the struct-level validation

@deankarn

Copy link
Copy Markdown
Contributor

I'm going to have to think about this one because tags are for validation or guiding validation but this adds in a new paradigm where tags outside of validations themselves can dynamically affect other aspects of the validation runtime.

There's also the matter to think about order and ability to override or needing to become a core part of the runtime like omitempty.

I'm thinking this maybe could be better handled in the RegisterTagNameFunc function. With a few minor tweaks to:

  1. Allow blank values in the cache extraction.
  2. Add a few checks to ensure if the alt name is blank not to append to the namespace.

Here is a quick example where I did just that custom, just for the below case a few more checks would be necessary, but you get the idea. There would also be a question of backward compatibility, technically RegisterTagNameFunc was already supposed to act like this but hasn't been for a long time.

image

@abemedia

Copy link
Copy Markdown
Contributor

@deankarn I implemented this behaviour in #1567

I'm going to have to think about this one because tags are for validation or guiding validation but this adds in a new paradigm where tags outside of validations themselves can dynamically affect other aspects of the validation runtime.

There's also the matter to think about order and ability to override or needing to become a core part of the runtime like omitempty.

I'm thinking this maybe could be better handled in the RegisterTagNameFunc function. With a few minor tweaks to:

  1. Allow blank values in the cache extraction.
  2. Add a few checks to ensure if the alt name is blank not to append to the namespace.

Here is a quick example where I did just that custom, just for the below case a few more checks would be necessary, but you get the idea. There would also be a question of backward compatibility, technically RegisterTagNameFunc was already supposed to act like this but hasn't been for a long time.

image

nodivbyzero pushed a commit that referenced this pull request May 26, 2026
## Fixes Or Enhances

When `RegisterTagNameFunc` returns `""` (typically for `json:"-"` or
equivalent), the field currently falls back to the struct field name in
error namespaces, defeating the tag-name remapping.

This PR adds the WithTagNameFuncBlankOmit option that preserves the
blank and omits the field from the error namespace - nested struct
levels collapse, and leaf field namespaces trim the trailing separator.

Gated behind an option for v10 backward compatibility. According to
@deankarn this was always meant to be the behaviour (see
#1426 (comment))
so I've gone off the assumption this will be the default in v11 and
followed the example of `WithRequiredStructEnabled`.

**Make sure that you've checked the boxes below before you submit PR:**
- [x] Tests exist or have been written that cover this particular
change.

@go-playground/validator-maintainers
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.

5 participants