Skip to content

Commit ff94dbd

Browse files
committed
Changed GetParser<T> signature
1 parent df517d6 commit ff94dbd

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

src/IGLib.Core/Console/Validation/Implementations/StringParsing/ParsableStringValidator.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,30 @@ namespace IGLib
99

1010
public class ParsableStringValidator<TTarget> : IValidator<string>
1111
{
12-
private static readonly IStringParser<TTarget> Parser = StringParserProvider.GetParser<TTarget>();
12+
private readonly IStringParser<TTarget> _parser;
1313

14-
public ParsableStringValidator(string? errorMessage = null)
14+
public ParsableStringValidator(
15+
IFormatProvider? formatProvider = null,
16+
string? errorMessage = null)
1517
{
18+
_parser = StringParserProvider.GetParser<TTarget>(formatProvider);
1619
ErrorMessage = errorMessage ?? $"Value is not a valid {typeof(TTarget).Name}.";
1720
}
1821

1922
public string ErrorMessage { get; }
2023

21-
public void Validate(string value, ValidationResults results)
24+
public virtual void Validate(string value, ValidationResults results)
2225
{
23-
// ArgumentNullException.ThrowIfNull(results);
24-
if (results == null)
25-
{ throw new ArgumentNullException(nameof(results), "Object to store validation results may not be null."); }
26+
// ToDo ArgumentNullException.ThrowIfNull(results);
2627

27-
if (value is null || !Parser.TryParse(value, out _))
28+
if (value == null)
29+
{
30+
results.AddError(ErrorMessage);
31+
return;
32+
}
33+
34+
if (!_parser.TryParse(value, out _))
2835
results.AddError(ErrorMessage);
2936
}
3037
}
31-
3238
}

src/IGLib.Core/Console/Validation/Implementations/StringParsing/StringParserProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ namespace IGLib
1111
public static class StringParserProvider
1212
{
1313

14-
public static IStringParser<T> GetParser<T>()
14+
public static IStringParser<T> GetParser<T>(IFormatProvider? formatProvider = null)
1515
{
1616
Type type = typeof(T);
1717

1818

1919

2020
throw new NotSupportedException($"Type {type.FullName} is not supported.");
21-
2221
}
2322

2423

0 commit comments

Comments
 (0)