AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.07k stars 350 forks source link

Fix multiple validator inconsistency #401

Closed fatihdumanli closed 2 years ago

fatihdumanli commented 2 years ago

Steps to reproduce:

  1. Run the program below
  2. Enter a string with a length shorter than 3 when prompted. You should get Sorry, your reply was invalid: value is too short. Min length is 3. Nothing surprising so far.
  3. Now enter a string that consists of more than 20 characters. You'll see that it passes the validation.

Expected behavior: A string with more than 20 characters shouldn't satisfy the conditions to be a valid answer.

        qCityName := survey.Question{
                Name: "cityname",
                Prompt: &survey.Input{
                        Message: "Enter a city name",
                },
                Validate: func(ans interface{}) error {
                        str, _ := ans.(string)
                        if len(str) > 20 {
                                return errors.New("Max length is 20")
                        }
                        return nil
                },
        }

        var cityName string
        survey.Ask([]*survey.Question{&qCityName}, &cityName, survey.WithValidator(survey.MinLength(3)))

This PR introduces a new change that rolls back the validation flow to the beginning as soon as we encounter a validation error. The current code is taking it from where it left off, ignoring the previous validators.