-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
Comments
You're not using your MyURL type in |
BTW this probably won't parse correctly, because your type alias doesn't have its |
I feel so dumb now that I didn't even check if I use my type... Anyway, much appreciated! |
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 Of course I could do the validation in 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)
} |
Does making it a pointer receiver fix it? |
No, Sir! Forgot to mention that I checked that, too. Rechecked now - does not make a difference. |
This seems to work fine for me: |
It seems to be a problem with environment variables: |
@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. |
Yep I think it is a bug too. A PR would be welcome, thanks. |
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. |
Actually yeah, a new issue would be preferable thanks. |
I do not think any issue (or fix) has been made for this? |
I made #390 as a followup. |
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?
The text was updated successfully, but these errors were encountered: