diff --git a/pkg/cmd/util/helpers.go b/pkg/cmd/util/helpers.go index 00d6ac98..16a679e3 100644 --- a/pkg/cmd/util/helpers.go +++ b/pkg/cmd/util/helpers.go @@ -407,6 +407,8 @@ func AddValidateFlags(cmd *cobra.Command) { "warn" will warn about unknown or duplicate fields without blocking the request if server-side field validation is enabled on the API server, and behave as "ignore" otherwise. "false" or "ignore" will not perform any schema validation, silently dropping any unknown or duplicate fields.`, ) + + cmd.Flags().Lookup("validate").NoOptDefVal = "strict" } func AddFilenameOptionFlags(cmd *cobra.Command, options *resource.FilenameOptions, usage string) { @@ -584,8 +586,6 @@ func GetValidationDirective(cmd *cobra.Command) (string, error) { b, err := strconv.ParseBool(validateFlag) if err != nil { switch validateFlag { - case cmd.Flag("validate").NoOptDefVal: - return metav1.FieldValidationStrict, nil case "strict": return metav1.FieldValidationStrict, nil case "warn": diff --git a/pkg/cmd/util/helpers_test.go b/pkg/cmd/util/helpers_test.go index 790b3669..6a695fd2 100644 --- a/pkg/cmd/util/helpers_test.go +++ b/pkg/cmd/util/helpers_test.go @@ -517,7 +517,9 @@ func TestGetValidationDirective(t *testing.T) { for _, tc := range tests { cmd := &cobra.Command{} AddValidateFlags(cmd) - cmd.Flags().Set("validate", tc.validateFlag) + if tc.validateFlag != "" { + cmd.Flags().Set("validate", tc.validateFlag) + } directive, err := GetValidationDirective(cmd) if directive != tc.expectedDirective { t.Errorf("validation directive, expected: %v, but got: %v", tc.expectedDirective, directive)