Skip to content

Multiple validators do not work correctly #1573

Closed

Description

@vlada-shubina

reproduced after d4cae87

given


        [Fact]
        public void Reproduce()
        {
            var command = new Command("new");
            var sub = new Command("install")
            {
                new Option<string>("--lang")
            };
            command.AddCommand(sub);
            command.AddOption(new Option<string>("--lang"));

            sub.AddValidator(result =>
            {
                result.ErrorMessage = "Wrong";
            });
            sub.AddValidator(result =>
            {
                // correct
            });

            var parseResult = ParserFactory.CreateParser(command).Parse("new install --lang b".Split(" "));
            var errorMessages = parseResult.Errors.Select(error => error.Message);
            Assert.NotEmpty(parseResult.Errors);
            Assert.Equal(1, parseResult.Errors.Count);  // fails, contains { "Wrong", "Wrong" }
        }

Activity

  1. vlada-shubina commented on Jan 10, 2022

    @vlada-shubina
    MemberAuthor

    root cause seems to be this chunk of code:

    for (var i = 0; i < command.Validators.Count; i++)
    {
    var validateSymbolResult = command.Validators[i];
    validateSymbolResult(_innermostCommandResult);
    if (!string.IsNullOrWhiteSpace(_innermostCommandResult.ErrorMessage))
    {
    AddErrorToResult(_innermostCommandResult, new ParseError(_innermostCommandResult.ErrorMessage!, _innermostCommandResult));
    }
    }

    it will repeat all the previous error messages for all remaining validators.

  2. jonsequitur commented on Jan 11, 2022

    @jonsequitur
    Contributor

    After looking at this a bit more closely, I'm not convinced the behavior is wrong.

    The behavior you see when setting ArgumentResult.ErrorMessage hasn't actually changed. What changed is we removed the string return value from the validator delegate which now forces the use of ArgumentResult.ErrorMessage, meaning now there's only one way to display an error. This was intentional. What I didn't anticipate was that because the same ArgumentResult instance is passed in turn to each validator, they also share a single ErrorMessage rather than each getting to contribute their own error messages. This was not intentional.

    Without changing the API, the old behavior can still be achieved: the second validator for example could append to the ErrorMessage that the previous one set. (I don't know if that's the right thing to do, but it's worth mentioning.)

    Another approach would be to change the API, e.g. replace the ErrorMessage property with something like AddErrorMessage, or a List<string> ErrorMessages. I'm not sure how valuable this extra level of granularity would be.

  3. vlada-shubina commented on Jan 11, 2022

    @vlada-shubina
    MemberAuthor

    Current behavior leads to the following output when using UseParseErrorReporting():

    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    Unrecognized command or argument(s): '--language','F#'.
    
    Description:
      Installs a template package.
    
    Usage:
      new3 install <package>... [options]
    
    Arguments:
      <package>  NuGet package ID or path to folder or NuGet package to install.
                 To install the NuGet package of certain version, use <package ID>::<version>.
    
    Options:
      --interactive                                Allows the command to stop and wait for user input or action (for
                                                   example to complete authentication).
      --add-source, --nuget-source <nuget-source>  Specifies a NuGet source to use during install.
      -?, -h, --help                               Show help and usage information
    
    

    If validator 1 fails with error, each next validator will append same error message, even though error message was not set. This leads to many duplicate messages in dotnet new case as it is using many validators. It was not the case using previous API.
    Imo if error message was not changed, it should not be probably added next time as new error then, because it is same error and should not be reported more then once.

  4. added a commit that references this issue on Jan 12, 2022
    6249aaf
  5. added a commit that references this issue on Mar 6, 2024
    7e5307c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions