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

after validate failure, answer value not output or clean #350

Open
zitn opened this issue Apr 16, 2021 · 6 comments
Open

after validate failure, answer value not output or clean #350

zitn opened this issue Apr 16, 2021 · 6 comments
Labels

Comments

@zitn
Copy link

zitn commented Apr 16, 2021

What operating system and terminal are you using?
macos iterm2 zsh
github.com/AlecAivazis/survey/v2 v2.2.9

An example that showcases the bug.

package main

import (
	"errors"
	"fmt"
	"net/url"

	"github.com/AlecAivazis/survey/v2"
)


func main() {
	prompt := &survey.Input{Message: "please input a url"}
	var answer url.URL
	err := survey.AskOne(prompt, &answer,survey.WithValidator(urlValidate))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(answer)
}

func urlValidate(val interface{}) error {
	urlStr, ok := val.(string)
	if !ok || urlStr == "" {
		return errors.New("please input correct url")
	}
	res, err := url.Parse(urlStr)
	if err != nil || res.Scheme == "" || res.Host == "" || res.Path == "" {
		return errors.New("please input correct url")
	}
	return nil
}

What did you expect to see?
Untitled
i inputed "aaa", then press Enter, the validate not passed, and the answer output has cleard, but when i continue to type "bbb", the "aaa" will show, continue press Enter, The answer is once again cleared, but when i input "http", the "aaabbb" will show. I think this is a confused behavior, is my code wrong? i also tested survey.Required, it has same behavior.
What did you see instead?
after validate failure, old answer should show or clean

@zitn zitn added the Bug label Apr 16, 2021
@AlecAivazis
Copy link
Owner

AlecAivazis commented Apr 16, 2021

Hey @zigitn - sorry you ran into this. If you want to try to dig a little deeper, here is where the validation loop happens and here is where the input is defined.

If you need any more help, feel free to reach out on the gophers slack!

@zitn
Copy link
Author

zitn commented Apr 16, 2021

i found a simple solution, in this file https://github.com/AlecAivazis/survey/blob/master/input.go
insert i.answer = "" to line 118
but I didn't test whether there is a non-expected impact on other parts, Because I didn't read all code, do you think this modification has non-expected impact?

@cben
Copy link
Contributor

cben commented May 9, 2021

I tested the #351 branch now with my app and it does fix the bug, which I've also been seeing 👍

@zigitn in the original test code, declaring var answer url.URL does not work — after validation passes, AskOne returns error could not find field matching. Declaring var answer string works and prints the accepted answer.
If you want to access the url.Parse() result, you should make the validator save it somewhere. Or parse the string a 2nd time after AskOne returns...

@vlad2095
Copy link

Hello guys, bug still exist

@zitn
Copy link
Author

zitn commented Jul 14, 2021

Hello guys, bug still exist

I did a simple fix before, #351, but I don’t have time to complete the test cases. If you want, you can take over my work.

@miniscruff
Copy link

I was not able to reproduce this with the latest version, is this fixed? I was gonna pick it up if it is still an issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants