[v3] `Validator` seems to always receive the empty string
zx8 opened this issue · comments
zx8 commented
Info:
- Using
v3.0.0-alpha7
Code
package main
import (
"context"
"fmt"
"log"
"os"
"slices"
"github.com/urfave/cli/v3"
)
func main() {
cmd := &cli.Command{
Name: "example",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "foo",
Validator: func(s string) error {
allowed := []string{"a", "b", "c"}
if slices.Contains(allowed, s) {
return nil
}
return fmt.Errorf("foo must be one of %s, not [%s]", allowed, s)
},
},
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}
Expected
$ go run . --foo=a
<no error>
Actual
$ go run . --foo=a
foo must be one of [a b c], not []
dearchap commented
@zx8 I see the issue. It is trying to validate the "default" value which in your case is "" since nothing is set. Can you set StringFlag.Value to "a"(say) and see what happens ?
zx8 commented
@dearchap You are correct. It seems to be validating the default rather than the supplied value.
dearchap commented
No it valids default value and if that succeeds then does the supplied value.
zx8 commented
Aha, ok. I'll add the default to the validator then! Thanks.