Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Immediate.Validations.Shared/ValidationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Immediate.Validations.Shared;
/// </summary>
public sealed partial class ValidationResult : IEnumerable<ValidationError>
{
[GeneratedRegex("{([^{}:]+)(?::([^{}]+))?}", RegexOptions.None, matchTimeoutMilliseconds: 50)]
[GeneratedRegex("{(?<name>[^{}:]+)(?::(?<format>[^{}]+))?}", RegexOptions.ExplicitCapture, matchTimeoutMilliseconds: 50)]
private static partial Regex FormatRegex();

private List<ValidationError>? _errors;
Expand Down Expand Up @@ -98,13 +98,13 @@ arguments is null
messageTemplate,
m =>
{
var key = m.Groups[1].Value;
var key = m.Groups["name"].Value;

if (!arguments.TryGetValue(key, out var value))
return m.Value;

if (m.Groups[2].Success)
return string.Format(provider: null, $"{{0:{m.Groups[2].Value}}}", value);
if (m.Groups["format"] is { Success: true, Value: var formatSpecifier })
return string.Format(provider: null, $"{{0:{formatSpecifier}}}", value);

return value?.ToString()!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Immediate.Validations.FunctionalTests.IntegrationTests;

public sealed class ValidationResultTests
public sealed partial class ValidationResultTests
{
public sealed partial record Command
{
Expand Down Expand Up @@ -472,4 +472,69 @@ public void PropertyNameTest8()
results
);
}

[Validate]
public sealed partial class MessageFormatCommand : IValidationTarget<MessageFormatCommand>
{
[LessThan(0.0d, Message = "{PropertyValue:N2}")]
public required double Value1 { get; init; }
[LessThan(0.0d, Message = "{Invalid}")]
public required double Value2 { get; init; }
}

[Test]
public void MessageFormatTest1()
{
var command = new MessageFormatCommand()
{
Value1 = 1,
Value2 = 1,
};

var results = MessageFormatCommand.Validate(command);

Assert.Equal(
[
new()
{
PropertyName = "Value1",
ErrorMessage = "1.00",
},
new()
{
PropertyName = "Value2",
ErrorMessage = "{Invalid}",
},
],
results
);
}

[Test]
public void MessageFormatTest2()
{
var command = new MessageFormatCommand()
{
Value1 = 1.2345,
Value2 = 1.2345,
};

var results = MessageFormatCommand.Validate(command);

Assert.Equal(
[
new()
{
PropertyName = "Value1",
ErrorMessage = "1.23",
},
new()
{
PropertyName = "Value2",
ErrorMessage = "{Invalid}",
},
],
results
);
}
}