File tree Expand file tree Collapse file tree
src/IGLib.Core/Console/Validation/Implementations/StringParsing Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments