diff --git a/src/ConsoleAppFramework/ParamsValidator.cs b/src/ConsoleAppFramework/ParamsValidator.cs index 82f36cd..9474cb4 100644 --- a/src/ConsoleAppFramework/ParamsValidator.cs +++ b/src/ConsoleAppFramework/ParamsValidator.cs @@ -22,6 +22,10 @@ public interface IParamsValidator /// public class ParamsValidator : IParamsValidator { + private readonly ConsoleAppOptions options; + + public ParamsValidator(ConsoleAppOptions options) => this.options = options; + /// ValidationResult? IParamsValidator.ValidateParameters( IEnumerable<(ParameterInfo Parameter, object? Value)> parameters) @@ -38,10 +42,13 @@ public class ParamsValidator : IParamsValidator var errorMessage = string.Join(Environment.NewLine, invalidParameters - .Select(tuple => $"{tuple.Parameter.Name!.ToLower()} ({tuple.Value}): {tuple.Result!.ErrorMessage}") + .Select(tuple => + $"{options.NameConverter(tuple.Parameter.Name!)} " + + $"({tuple.Value}): " + + $"{tuple.Result!.ErrorMessage}") ); - return new ValidationResult($"Some parameters have invalid value:{Environment.NewLine}{errorMessage}"); + return new ValidationResult($"Some parameters have invalid values:{Environment.NewLine}{errorMessage}"); } private static ValidationResult? Validate(ParameterInfo parameterInfo, object? value) diff --git a/tests/ConsoleAppFramework.Tests/Integration/ValidationAttributeTests.cs b/tests/ConsoleAppFramework.Tests/Integration/ValidationAttributeTests.cs index 9ee59f5..692f323 100644 --- a/tests/ConsoleAppFramework.Tests/Integration/ValidationAttributeTests.cs +++ b/tests/ConsoleAppFramework.Tests/Integration/ValidationAttributeTests.cs @@ -1,8 +1,8 @@ using System; using System.ComponentModel.DataAnnotations; using FluentAssertions; -using Microsoft.Extensions.Hosting; using Xunit; + // ReSharper disable UnusedMember.Global // ReSharper disable UnusedParameter.Global @@ -22,20 +22,44 @@ public void Validate_String_Length_Test() const string optionValue = "too-large-string-value"; var args = new[] { nameof(AppWithValidationAttributes.StrLength), $"--{optionName}", optionValue }; - Host.CreateDefaultBuilder().RunConsoleAppFrameworkAsync(args); + ConsoleApp.Run(args); - // Validation fails, so StrLength command is not executed. - console.Output.Should().NotContain(AppWithValidationAttributes.StrLengthOutput); + // Validation should fail, so StrLength command should not be executed. + console.Output.Should().NotContain(AppWithValidationAttributes.Output); console.Output.Should().Contain(optionName); console.Output.Should().Contain(optionValue); } + [Fact] + public void Command_With_Multiple_Params() + { + using var console = new CaptureConsoleOutput(); + + var args = new[] + { + nameof(AppWithValidationAttributes.MultipleParams), + "--second-arg", "10", + "--first-arg", "invalid-email-address" + }; + + ConsoleApp.Run(args); + + // Validation should fail, so StrLength command should not be executed. + console.Output.Should().NotContain(AppWithValidationAttributes.Output); + } + /// internal class AppWithValidationAttributes : ConsoleAppBase { - public const string StrLengthOutput = $"hello from {nameof(StrLength)}"; + public const string Output = $"hello from {nameof(AppWithValidationAttributes)}"; + + [Command(nameof(StrLength))] + public void StrLength([StringLength(maximumLength: 8)] string arg) => Console.WriteLine(Output); - public void StrLength([StringLength(maximumLength: 8)] string arg) => Console.WriteLine(StrLengthOutput); + [Command(nameof(MultipleParams))] + public void MultipleParams( + [EmailAddress] string firstArg, + [Range(0, 2)] int secondArg) => Console.WriteLine(Output); } } \ No newline at end of file