@@ -6,66 +6,64 @@ namespace Triplex.Validations.ArgumentsHelpers;
66internal static class Extensions
77{
88 [ return : NotNull ]
9- internal static T ValueOrThrowIfNull < T > ( [ NotNull ] this T ? value ,
9+ internal static T Check < T > ( [ NotNull ] this T ? value ,
1010 [ CallerArgumentExpression ( nameof ( value ) ) ] string paramName = "" )
1111 => value ?? throw new ArgumentNullException ( paramName ) ;
1212
1313 [ return : NotNull ]
14- internal static T ValueOrThrowIfNull < T > ( [ NotNull ] this T ? value , string paramName , string customMessage )
14+ internal static T CheckWithParamName < T > ( [ NotNull ] this T ? value , string paramName )
15+ => value ?? throw new ArgumentNullException ( paramName ) ;
16+
17+ [ return : NotNull ]
18+ internal static T CheckWithParamName < T > ( [ NotNull ] this T ? value , string paramName , string customMessage )
1519 => value ?? throw new ArgumentNullException ( paramName , customMessage ) ;
1620
1721 [ return : NotNull ]
18- internal static T ValueOrThrowInvalidOperationIfNull < T > ( [ NotNull ] this T ? stateElement ,
22+ internal static T CheckOrInvalidOperationException < T > ( [ NotNull ] this T ? stateElement ,
1923 string elementName )
2024 => stateElement
2125 ?? throw new InvalidOperationException ( $ "Operation not allowed when { elementName } is null.") ;
2226
2327 [ return : NotNull ]
24- internal static string ValueOrThrowIfZeroLength ( this string value , string paramName )
25- => ValueOrThrowIfZeroLength ( value , paramName , "Can not be empty (zero length)." ) ;
28+ internal static string CheckNotZeroLength ( [ NotNull ] this string ? value , [ CallerArgumentExpression ( nameof ( value ) ) ] string paramName = "" )
29+ => CheckWithParamName ( value , paramName )
30+ . DoCheckNotZeroLength ( paramName , "Can not be empty (zero length)." ) ;
31+
32+ [ return : NotNull ]
33+ internal static string CheckNotZeroLength ( [ NotNull ] this string ? value , string paramName , string customMessage )
34+ => CheckWithParamName ( value , paramName , customMessage )
35+ . DoCheckNotZeroLength ( paramName , customMessage ) ;
2636
2737 [ return : NotNull ]
28- internal static string ValueOrThrowIfZeroLength ( this string value , string paramName , string customMessage )
38+ private static string DoCheckNotZeroLength ( this string value , string paramName , string customMessage )
2939 => value . Length is not 0
3040 ? value
3141 : throw new ArgumentFormatException ( paramName : paramName , message : customMessage ) ;
3242
3343 [ return : NotNull ]
34- internal static string ValueOrThrowIfWhiteSpaceOnly ( this string value , string paramName )
35- => ValueOrThrowIfWhiteSpaceOnly ( value , paramName , "Can not be white-space only." ) ;
44+ internal static string CheckNotWhiteSpaceOnly ( this string value , string paramName )
45+ => CheckNotWhiteSpaceOnly ( value , paramName , "Can not be white-space only." ) ;
3646
3747 [ return : NotNull ]
38- internal static string ValueOrThrowIfWhiteSpaceOnly ( this string value , string paramName , string customMessage )
48+ internal static string CheckNotWhiteSpaceOnly ( this string value , string paramName , string customMessage )
3949 => value . Any ( ch => ch . IsNotWhiteSpace ( ) )
4050 ? value
4151 : throw new ArgumentFormatException ( paramName : paramName , message : customMessage ) ;
4252
4353 [ return : NotNull ]
44- internal static string ValueOrThrowIfNullOrZeroLength ( [ NotNull ] this string ? value ,
45- string paramName )
46- => ValueOrThrowIfNull ( value , paramName )
47- . ValueOrThrowIfZeroLength ( paramName ) ;
48-
49- [ return : NotNull ]
50- internal static string ValueOrThrowIfNullOrZeroLength ( [ NotNull ] this string ? value ,
51- string paramName , string customMessage )
52- => ValueOrThrowIfNull ( value , paramName , customMessage )
53- . ValueOrThrowIfZeroLength ( paramName , customMessage ) ;
54-
55- [ return : NotNull ]
56- internal static string ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly (
54+ internal static string CheckNotZeroLengthOrWhiteSpaceOnly (
5755 [ NotNull ] this string ? value ,
5856 [ CallerArgumentExpression ( nameof ( value ) ) ] string paramName = "" )
59- => ValueOrThrowIfNull ( value , paramName )
60- . ValueOrThrowIfZeroLength ( paramName )
61- . ValueOrThrowIfWhiteSpaceOnly ( paramName ) ;
57+ => Check ( value , paramName )
58+ . CheckNotZeroLength ( paramName )
59+ . CheckNotWhiteSpaceOnly ( paramName ) ;
6260
6361 [ return : NotNull ]
6462 internal static string ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly ( [ NotNull ] this string ? value ,
6563 string paramName , string customMessage )
66- => ValueOrThrowIfNull ( value , paramName , customMessage )
67- . ValueOrThrowIfZeroLength ( paramName , customMessage )
68- . ValueOrThrowIfWhiteSpaceOnly ( paramName , customMessage ) ;
64+ => CheckWithParamName ( value , paramName , customMessage )
65+ . DoCheckNotZeroLength ( paramName , customMessage )
66+ . CheckNotWhiteSpaceOnly ( paramName , customMessage ) ;
6967
7068 internal static bool IsNotWhiteSpace ( this char ch ) => ! char . IsWhiteSpace ( ch ) ;
7169
@@ -97,9 +95,29 @@ internal static TEnumType ValueOrThrowIfNotDefined<TEnumType>(this TEnumType val
9795 internal static TType [ ] ValueOrThrowIfNullOrWithLessThanElements < TType > (
9896 [ NotNull ] this TType [ ] ? value , int minimumElements , string paramName )
9997 {
100- _ = OutOfRangeChecks . GreaterThanOrEqualTo ( ValueOrThrowIfNull ( value , paramName ) . Length , minimumElements , paramName ) ;
98+ _ = OutOfRangeChecks . GreaterThanOrEqualTo ( Check ( value , paramName ) . Length , minimumElements , paramName ) ;
10199
102100 return value ! ;
103101 }
102+
103+ #region ParameterName and Exception Messages Helpers
104+
105+ [ return : NotNull ]
106+ internal static string CheckParamName (
107+ [ NotNull ] this string ? value ,
108+ [ CallerArgumentExpression ( nameof ( value ) ) ] string paramName = "" )
109+ => CheckWithParamName ( value , paramName )
110+ . CheckNotZeroLength ( paramName )
111+ . CheckNotWhiteSpaceOnly ( paramName ) ;
112+
113+ [ return : NotNull ]
114+ internal static string CheckExceptionMessage (
115+ [ NotNull ] this string ? value ,
116+ [ CallerArgumentExpression ( nameof ( value ) ) ] string paramName = "" )
117+ => CheckWithParamName ( value , paramName )
118+ . CheckNotZeroLength ( paramName )
119+ . CheckNotWhiteSpaceOnly ( paramName ) ;
120+
121+ #endregion
104122}
105123#pragma warning restore CA1303 // Do not pass literals as localized parameters
0 commit comments