Skip to content

Commit 32da49a

Browse files
committed
Improved ParsableStringValidator according to newer developments
1 parent 758052f commit 32da49a

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel.DataAnnotations;
4+
using System.Globalization;
45
using System.Linq;
56
using System.Text;
67

7-
namespace IGLib
8+
namespace IGLib;
9+
10+
public class ParsableStringValidator<TTarget> : IValidator<string>
811
{
12+
private readonly IStringParser<TTarget> _parser;
913

10-
public class ParsableStringValidator<TTarget> : IValidator<string>
14+
public ParsableStringValidator(
15+
IFormatProvider formatProvider = null,
16+
string errorMessage = null)
1117
{
12-
private readonly IStringParser<TTarget> _parser;
18+
_parser = StringParserProvider.GetParser<TTarget>(formatProvider ?? CultureInfo.CurrentCulture);
19+
ErrorMessage = errorMessage ?? $"Value is not a valid {typeof(TTarget).Name}.";
20+
}
1321

14-
public ParsableStringValidator(
15-
IFormatProvider? formatProvider = null,
16-
string? errorMessage = null)
17-
{
18-
_parser = StringParserProvider.GetParser<TTarget>(formatProvider);
19-
ErrorMessage = errorMessage ?? $"Value is not a valid {typeof(TTarget).Name}.";
20-
}
22+
public string ErrorMessage { get; }
2123

22-
public string ErrorMessage { get; }
24+
public virtual void Validate(string value, ValidationResults results)
25+
{
26+
if (results == null)
27+
throw new ArgumentNullException(nameof(results));
2328

24-
public virtual void Validate(string value, ValidationResults results)
29+
if (value == null)
2530
{
26-
// ToDo ArgumentNullException.ThrowIfNull(results);
27-
28-
if (value == null)
29-
{
30-
results.AddError(ErrorMessage);
31-
return;
32-
}
33-
34-
if (!_parser.TryParse(value, out _))
35-
results.AddError(ErrorMessage);
31+
results.AddError(ErrorMessage);
32+
return;
3633
}
34+
35+
if (!_parser.TryParse(value, out _))
36+
results.AddError(ErrorMessage);
3737
}
3838
}

0 commit comments

Comments
 (0)