Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

add support for 'required' tag value#167

Closed
bonifaido wants to merge 1 commit intomitchellh:masterfrom
bonifaido:required
Closed

add support for 'required' tag value#167
bonifaido wants to merge 1 commit intomitchellh:masterfrom
bonifaido:required

Conversation

@bonifaido
Copy link
Copy Markdown

Implementation for checking that required fields got filled in map -> struct decoding direction.

A basic test has been added as well.

type Required struct {
	RequiredBar string `mapstructure:"bar,required"`
	Value       string `mapstructure:"foo"`
}

Fixes: #7

@janderland
Copy link
Copy Markdown

Would be nice if this could be set globally as well, for all fields.

@philippfrank
Copy link
Copy Markdown

@bonifaido I was looking exactly for this. What's the status?

@bonifaido
Copy link
Copy Markdown
Author

I need to rebase on master now I guess, since this is almost 1 year old, after that I'm waiting on a review/merge 🙂

@bonifaido bonifaido force-pushed the required branch 2 times, most recently from 39194a0 to 1a6dfa8 Compare May 6, 2020 09:19
@bonifaido
Copy link
Copy Markdown
Author

Rebased it on latest master.

@philippfrank
Copy link
Copy Markdown

@mitchellh Are there any plans to merge/review this? It's a really helpful addition!

@dz0gchen
Copy link
Copy Markdown

need merge

@wang502
Copy link
Copy Markdown

wang502 commented May 19, 2020

this is feature is really helpful. can we give this a review? thanks :) @mitchellh

@montaro
Copy link
Copy Markdown

montaro commented Jun 14, 2020

Please merge

@wangshuai14
Copy link
Copy Markdown

It seems not working. Are there errors in my code?
image

@bonifaido
Copy link
Copy Markdown
Author

Are you using my fork@branch @wangshuai14 ?

@wangshuai14
Copy link
Copy Markdown

Are you using my fork@branch @wangshuai14 ?

Got it! thanks

@bonifaido
Copy link
Copy Markdown
Author

bonifaido commented Sep 17, 2020

Just rebased on the latest master to see its working.

@Etran-H
Copy link
Copy Markdown

Etran-H commented Jan 18, 2022

@mitchellh
can comment on this?

@cmatthias
Copy link
Copy Markdown

If it's helpful to anyone, I forked mapstructure and merged this PR as well as #225 into my fork. Feel free to use.

https://github.com/cmatthias/mapstructure

@mitchellh
Copy link
Copy Markdown
Owner

Hello all. Its been 3 years, and I've sat on this. I've integrated mapstructure into a dozen or more projects by this point. I've certainly run into this issue before. I believe that its better to use a full featured struct validation library rather than support this one feature in mapstructure. I've always reached for something like ozzo-validation or validator and felt that was just the better approach. I'm sorry, but I want to keep this library as focused on the decoding problem rather than data validation (beyond simple type matching -- necessary for decoding).

@mitchellh
Copy link
Copy Markdown
Owner

Actually #225 is a great approach to this that I think balances the line between enabling this sort of functionality but not stepping into what in my opinion is validation territory in the same way. I'll merge that.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add config to error if field is NOT set

10 participants