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

Custom validators do not run for envars #224

Closed
dreamscached opened this issue Oct 17, 2021 · 14 comments
Closed

Custom validators do not run for envars #224

dreamscached opened this issue Oct 17, 2021 · 14 comments

Comments

@dreamscached
Copy link

dreamscached commented Oct 17, 2021

Hello! I want to validate an input URL and I did that per this section in the README. However, it doesn't seem that MyURL#Validate is ever getting invoked. What am I doing wrong?

type MyURL url.URL

func (u *MyURL) Validate() error {
	if u.Host != "example.com" {
		return errors.New("not an example.com URL")
	}
	return nil
}

var args struct {
	URL    []*url.URL `arg:"" help:"List of URL."`
	Folder string     `arg:"" optional:"" help:"Output folder.`
}

func main() {
	kong.Parse(&args)
}
@alecthomas
Copy link
Owner

You're not using your MyURL type in args.

@alecthomas
Copy link
Owner

alecthomas commented Oct 17, 2021

BTW this probably won't parse correctly, because your type alias doesn't have its TextUnmarshaler method. Go type aliases don't retain the original type's methods.

@dreamscached
Copy link
Author

I feel so dumb now that I didn't even check if I use my type... Anyway, much appreciated!

@mwmahlberg
Copy link

I am not quite sure, but this seems to be related. When trying to custom-validate an enum (I neither can have default air required set on this), there is no validation error in this code.

The TextUnmarshalerworks fine, since any arbitrary value for Hoststate is accepted.

Of course I could do the validation in UnmarshalText (tested, works fine, ofc), but that seems rather ugly to me.

Am I missing something here?

type HostState string

func (hs HostState) Validate() error {
        // For Illustration purposes
	fmt.Println("Not printed")
	switch hs {
	case Up, Down, Unreachable:
		return nil
	default:
		return errors.New("Must be UP, DOWN or UNREACH")
	}
}

func (hs *HostState) UnmarshalText(in []byte) error {
	*hs = HostState(string(in))
	return nil
}

const (
	Up          HostState = "UP"
	Down        HostState = "DOWN"
	Unreachable HostState = "UNREACH"
)

type config struct {
	HostState HostState `env:"HOSTSTATE" default:"DOWN"`
}

var cli config

func main() {
	ctx := kong.Parse(&cli)
	ctx.Printf("Config: %#v", cli)
}

@alecthomas
Copy link
Owner

Does making it a pointer receiver fix it?

@mwmahlberg
Copy link

No, Sir! Forgot to mention that I checked that, too. Rechecked now - does not make a difference.

@alecthomas
Copy link
Owner

This seems to work fine for me:
https://go.dev/play/p/QHiLtLPAjzz

@mwmahlberg
Copy link

It seems to be a problem with environment variables:
https://go.dev/play/p/E4WbmsyUfr3

@mwmahlberg
Copy link

@alecthomas I think this is a bug.

As per above code, custom validators for environment variables are not triggered. Shall I open a new bug ticket on this?

I will dig into the code and try to provide a PR in any case.

@alecthomas
Copy link
Owner

Yep I think it is a bug too. A PR would be welcome, thanks.

@alecthomas alecthomas reopened this Mar 11, 2023
@alecthomas
Copy link
Owner

One challenge might be that envars are unfortunately special cased in Kong, so fixing it might be a bit of an issue. I have been meaning to fix the underlying issue for a while, but it's generally not been too much of a problem so it's been low priority.

@alecthomas alecthomas changed the title Cannot make a custom validator Custom validators do not run for envars Mar 11, 2023
@alecthomas
Copy link
Owner

Actually yeah, a new issue would be preferable thanks.

@mitar
Copy link
Contributor

mitar commented Nov 28, 2023

I do not think any issue (or fix) has been made for this?

@mitar
Copy link
Contributor

mitar commented Nov 29, 2023

I made #390 as a followup.

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

No branches or pull requests

4 participants