Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Fix multiple validator inconsistency #401

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix on multiple validators
  • Loading branch information
fatihdumanli committed Jan 23, 2022
commit 3e0b413ad35c236f2157ce0718a2de962f686c97
16 changes: 11 additions & 5 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package survey
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"strings"
Expand Down Expand Up @@ -323,12 +324,14 @@ func Ask(qs []*Question, response interface{}, opts ...AskOpt) error {
validators = append(validators, validator)
}

// apply every validator to thte response
for _, validator := range validators {
fmt.Printf("%d validator in total", len(validators))

// apply every validator to the response
for i := 0; i < len(validators); i++ {
currentValidator := validators[i]
// wait for a valid response
for invalid := validator(ans); invalid != nil; invalid = validator(ans) {
for invalid := currentValidator(ans); invalid != nil; invalid = currentValidator(ans) {
err := q.Prompt.Error(&options.PromptConfig, invalid)
// if there was a problem
if err != nil {
return err
}
Expand All @@ -339,10 +342,13 @@ func Ask(qs []*Question, response interface{}, opts ...AskOpt) error {
} else {
ans, err = q.Prompt.Prompt(&options.PromptConfig)
}
// if there was a problem
if err != nil {
return err
}

//start over from first validator upon getting the new answer.
i = -1
break
}
}

Expand Down