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

TextPrompt.Validate does not chain #1742

Open
simishag opened this issue Jan 24, 2025 · 2 comments
Open

TextPrompt.Validate does not chain #1742

simishag opened this issue Jan 24, 2025 · 2 comments

Comments

@simishag
Copy link

Information

  • OS: Windows
  • Version: 0.49.1
  • Terminal: Windows Terminal

Describe the bug
TextPrompt.Validate() does not chain with itself. Only the last call to Validate() will run. Any other validators are ignored. This is clear in TextPromptExtensions where obj.Validator is simply replaced each time Validate() is called.

To Reproduce
Chain multiple Validate() calls together. Only the last call will run, regardless of any previous calls.

Expected behavior
If Validate() is used multiple times, each Validator should run. Otherwise, comments should be revised to make it clear that only 1 call to Validate() is allowed.

Additional context
XML comments in TextPromptExtensions indicate that Validate() should chain, and a comment in TextPrompt says "Run all validators". To me, this indicates that multiple validators are allowed and should work as expected.

Multiple Validate() calls would be useful to separate validation logic, and especially to tailor validation errors more specifically. With a single call, multiple checks all have to run at once and the error message must suggest all possible errors.

Semantics are debatable: should validation stop on the first failure, or run all validators and present all failures? Could be configurable.

I am willing to implement this and open a PR if this is a desirable change.


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

@simishag simishag added bug Something isn't working needs triage labels Jan 24, 2025
@github-project-automation github-project-automation bot moved this to Todo 🕑 in Spectre Console Jan 24, 2025
@patriksvensson
Copy link
Contributor

Could you provide a code example that demonstrates this?

@simishag
Copy link
Author

Sure, this is taken from a current project:

// regex & IsValidDateString() are defined elsewhere
var StartingDate = AnsiConsole.Prompt(
        new TextPrompt<string>("Enter starting date as MMDD, MMDDHH, MMDDHHmm, or yyyyMMDDHHmm)):")
            .Validate(s => true,  "Early true")
            .Validate(s => false, "Early false")
            .Validate(s => regex.IsMatch(s), "Date string is unparseable")
            .Validate(s => true,  "Middle true")
            .Validate(s => false, "Middle false")
            .Validate(s => IsValidDateString(s, out startingDateActual), "Date string represents an invalid date")
            .Validate(s => true,  "Late true")
            .Validate(s => false, "Late false")
        );  

The above will always reject any input with error message "Late false". Everything else will be ignored. Removing the very last Validate() will always return Success regardless of the input string. Removing the last 2 Validate() will return the expected result and error message (if appropriate) from IsValidDateString().

I was able to work around it like so, but this is not desirable because the error message has to report all possible problems rather than a single one:

var StartingDate = AnsiConsole.Prompt(
        new TextPrompt<string>("Enter starting date as MMDD, MMDDHH, MMDDHHmm, or yyyyMMDDHHmm)):")
            .Validate(s => regex.IsMatch(s) && IsValidDateString(s, out startingDateActual),
                "Date is unparseable or represents an invalid date")
    );

@patriksvensson patriksvensson added feature and removed bug Something isn't working labels Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo 🕑
Development

No branches or pull requests

2 participants