Skip to content

Conversation

@m1k1o
Copy link

@m1k1o m1k1o commented Jun 14, 2025

Trying to merge changes in my fork of mapstructure, maybe you will be interested in such feature.

ValidateHook, if set, will be called after decoding is complete. This is useful for types that need a finalization step or validation. The ValidateHook is called for every map and value in the input. This means that if a struct has embedded fields with squash tags the post decode hook is called only once with all of the input data, not once for each embedded struct. If an error is returned, the entire decode will fail with that error.

@sagikazarmark
Copy link
Member

Thanks for opening a PR!

TBH, I'm a bit conflicted about this change. It feels out of scope for the library.

I usually just write Validate or PostProcess functions on the structs themselves.

How much work would it be to externalize this feature to another library?

Maybe I'm missing something?

@pcfreak30
Copy link

Tbh for validation I use zog. I have overall a system that uses https://github.com/knadh/koanf internally, which uses mapstructure, and https://github.com/Oudwins/zog which CAN do decoding but is basically zod JS for go.

I kind of agree it is definitely possible to just validate an object after fully decoding it unless there is a very good reason not to?

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.

3 participants