Skip to content

Commit

Permalink
Create flag.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ayala-orca authored Nov 21, 2024
1 parent de385bd commit 1f22b00
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package validator

import (
"fmt"
"reflect"
"strings"

"golang.org/x/exp/slices"

"github.com/orcasecurity/shiftleft-cli/lib/utils"
)

type FlagDependencyValidator[T any] struct {
}

func (v FlagDependencyValidator[T]) Validate(cmdOptions T, dependentField string, dependencyField string, allowedValues []string) error {
cmd := reflect.ValueOf(cmdOptions)
dependentFieldValue := reflect.Indirect(cmd).FieldByName(dependentField)
dependencyFieldValue := reflect.Indirect(cmd).FieldByName(dependencyField)
if slices.Contains(allowedValues, "") && dependencyFieldValue.IsZero() {
return nil
}
allowedValues = utils.RemoveFromSlice(allowedValues, "")
if !dependentFieldValue.IsZero() && !slices.Contains(allowedValues, dependencyFieldValue.String()) {
allowedValues := strings.Join(allowedValues, ",")
return fmt.Errorf("input error - '%s' option can be used only with %s=%s", dependentField, dependencyField, allowedValues)
}

return nil
}

0 comments on commit 1f22b00

Please sign in to comment.