Skip to content

Commit

Permalink
Corrected return type of validater function from error to bool
Browse files Browse the repository at this point in the history
Signed-off-by: parauliya <aman@infracloud.io>
  • Loading branch information
parauliya committed Sep 9, 2020
1 parent ea4bfd5 commit e5f413a
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions pkg/template_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ func ParseYAML(yamlContent []byte) (*Workflow, error) {
func ValidateTemplate(wf *Workflow) error {
taskNameMap := make(map[string]struct{})
for _, task := range wf.Tasks {
err := hasValidLength(task.Name)
if err != nil {
return err
if hasEmptyName(task.Name) {
return errors.New(errEmptyName + "task name")
}
if !hasValidLength(task.Name) {
return errors.New(errInvalidLength + task.Name)
}
_, ok := taskNameMap[task.Name]
if ok {
Expand All @@ -42,18 +44,20 @@ func ValidateTemplate(wf *Workflow) error {
taskNameMap[task.Name] = struct{}{}
actionNameMap := make(map[string]struct{})
for _, action := range task.Actions {
err := hasValidLength(action.Name)
if err != nil {
return err
if hasEmptyName(action.Name) {
return errors.New(errEmptyName + "action name")
}

if !hasValidLength(action.Name) {
return errors.New(errInvalidLength + action.Name)
}
err = isValidImageName(action.Image)
if err != nil {

if !hasValidImageName(action.Image) {
return errors.New(errInvalidActionImage + action.Image)
}

err = hasValidTimeout(action.Timeout, action.Name)
if err != nil {
return err
if !hasValidTimeout(action.Timeout) {
return errors.New(errNoTimeout + action.Name)
}
_, ok := actionNameMap[action.Name]
if ok {
Expand All @@ -65,27 +69,18 @@ func ValidateTemplate(wf *Workflow) error {
return nil
}

func hasValidLength(name string) error {
if name == "" {
return errors.New(errEmptyName + name)
}
if len(name) > 200 {
return errors.New(errInvalidLength + name)
}
return nil
func hasEmptyName(name string) bool {
return name == ""
}
func hasValidLength(name string) bool {
return len(name) < 200
}

func isValidImageName(name string) error {
func hasValidImageName(name string) bool {
_, err := reference.ParseNormalizedNamed(name)
if err != nil {
return err
}
return nil
return err == nil
}

func hasValidTimeout(timeout int64, name string) error {
if timeout > 0 {
return nil
}
return errors.New(errNoTimeout + name)
func hasValidTimeout(timeout int64) bool {
return timeout > 0
}

0 comments on commit e5f413a

Please sign in to comment.