Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Prompt] Invoke validators on default value #1343

Open
w4po opened this issue Oct 24, 2023 · 0 comments
Open

[Prompt] Invoke validators on default value #1343

w4po opened this issue Oct 24, 2023 · 0 comments

Comments

@w4po
Copy link

w4po commented Oct 24, 2023

I have encountered a use case in which I'd like the Prompt to validate the default value. I want to verify whether a file exists based on the provided value/default value.

I've found that when a user simply presses Enter, the validation is not triggered:

var path = AnsiConsole.Prompt(
            new TextPrompt<string>("Enter [olive]file path[/]: ")
                .DefaultValue("emails.txt")
                .HideDefaultValue()
                .ValidationErrorMessage("[red]Wrong path, File does not exist![/]")
                .Validate(File.Exists));

Possible solution:

if (DefaultValue != null)
{
var defaultValue = converter(DefaultValue.Value);
console.Write(IsSecret ? defaultValue.Mask(Mask) : defaultValue, promptStyle);
console.WriteLine();
return DefaultValue.Value;
}

Change to:

if (DefaultValue != null)
{
    var defaultValue = converter(DefaultValue.Value);
    console.Write(IsSecret ? defaultValue.Mask(Mask) : defaultValue, promptStyle);
    console.WriteLine();
    
    // Run all validators
    if (!ValidateResult(defaultValue , out var validationMessage))
    {
        console.MarkupLine(validationMessage);
        WritePrompt(console);
        continue;
    }
    
    return DefaultValue.Value;
}

Please upvote 👍 this issue if you are interested in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo 🕑
Development

No branches or pull requests

1 participant