Skip to content

Commit ec736de

Browse files
committed
name conventions in ParamsValidator
1 parent 3a09d86 commit ec736de

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/ConsoleAppFramework/ParamsValidator.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public interface IParamsValidator
2222
/// <inheritdoc />
2323
public class ParamsValidator : IParamsValidator
2424
{
25+
private readonly ConsoleAppOptions options;
26+
27+
public ParamsValidator(ConsoleAppOptions options) => this.options = options;
28+
2529
/// <inheritdoc />
2630
ValidationResult? IParamsValidator.ValidateParameters(
2731
IEnumerable<(ParameterInfo Parameter, object? Value)> parameters)
@@ -38,10 +42,13 @@ public class ParamsValidator : IParamsValidator
3842

3943
var errorMessage = string.Join(Environment.NewLine,
4044
invalidParameters
41-
.Select(tuple => $"{tuple.Parameter.Name!.ToLower()} ({tuple.Value}): {tuple.Result!.ErrorMessage}")
45+
.Select(tuple =>
46+
$"{options.NameConverter(tuple.Parameter.Name!)} " +
47+
$"({tuple.Value}): " +
48+
$"{tuple.Result!.ErrorMessage}")
4249
);
4350

44-
return new ValidationResult($"Some parameters have invalid value:{Environment.NewLine}{errorMessage}");
51+
return new ValidationResult($"Some parameters have invalid values:{Environment.NewLine}{errorMessage}");
4552
}
4653

4754
private static ValidationResult? Validate(ParameterInfo parameterInfo, object? value)

tests/ConsoleAppFramework.Tests/Integration/ValidationAttributeTests.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.ComponentModel.DataAnnotations;
33
using FluentAssertions;
4-
using Microsoft.Extensions.Hosting;
54
using Xunit;
5+
66
// ReSharper disable UnusedMember.Global
77
// ReSharper disable UnusedParameter.Global
88

@@ -22,20 +22,44 @@ public void Validate_String_Length_Test()
2222
const string optionValue = "too-large-string-value";
2323

2424
var args = new[] { nameof(AppWithValidationAttributes.StrLength), $"--{optionName}", optionValue };
25-
Host.CreateDefaultBuilder().RunConsoleAppFrameworkAsync<AppWithValidationAttributes>(args);
25+
ConsoleApp.Run<AppWithValidationAttributes>(args);
2626

27-
// Validation fails, so StrLength command is not executed.
28-
console.Output.Should().NotContain(AppWithValidationAttributes.StrLengthOutput);
27+
// Validation should fail, so StrLength command should not be executed.
28+
console.Output.Should().NotContain(AppWithValidationAttributes.Output);
2929

3030
console.Output.Should().Contain(optionName);
3131
console.Output.Should().Contain(optionValue);
3232
}
3333

34+
[Fact]
35+
public void Command_With_Multiple_Params()
36+
{
37+
using var console = new CaptureConsoleOutput();
38+
39+
var args = new[]
40+
{
41+
nameof(AppWithValidationAttributes.MultipleParams),
42+
"--second-arg", "10",
43+
"--first-arg", "invalid-email-address"
44+
};
45+
46+
ConsoleApp.Run<AppWithValidationAttributes>(args);
47+
48+
// Validation should fail, so StrLength command should not be executed.
49+
console.Output.Should().NotContain(AppWithValidationAttributes.Output);
50+
}
51+
3452
/// <inheritdoc />
3553
internal class AppWithValidationAttributes : ConsoleAppBase
3654
{
37-
public const string StrLengthOutput = $"hello from {nameof(StrLength)}";
55+
public const string Output = $"hello from {nameof(AppWithValidationAttributes)}";
56+
57+
[Command(nameof(StrLength))]
58+
public void StrLength([StringLength(maximumLength: 8)] string arg) => Console.WriteLine(Output);
3859

39-
public void StrLength([StringLength(maximumLength: 8)] string arg) => Console.WriteLine(StrLengthOutput);
60+
[Command(nameof(MultipleParams))]
61+
public void MultipleParams(
62+
[EmailAddress] string firstArg,
63+
[Range(0, 2)] int secondArg) => Console.WriteLine(Output);
4064
}
4165
}

0 commit comments

Comments
 (0)