Skip to content

Commit 87af35d

Browse files
Lorenzo Solano MartinezLorenzo Solano Martinez
authored andcommitted
Refactor Extensions
1 parent 84a248b commit 87af35d

File tree

7 files changed

+117
-95
lines changed

7 files changed

+117
-95
lines changed

src/Validations/Algorithms/Checksum/LuhnFormula.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static bool IsValid([NotNull] string? fullDigits)
6666
[return: NotNull]
6767
private static string ValidateDigitsAsString([NotNull] string? fullDigits)
6868
{
69-
string notNullDigits = fullDigits.ValueOrThrowIfNullOrZeroLength(nameof(fullDigits));
69+
string notNullDigits = fullDigits.CheckNotZeroLength(nameof(fullDigits));
7070

7171
return notNullDigits.Length switch
7272
{

src/Validations/Arguments.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static partial class Arguments
2525
public static TParamType OrException<TParamType>(
2626
[NotNull] TParamType? value,
2727
[NotNull, CallerArgumentExpression(nameof(value))] string paramName = "") where TParamType : class
28-
=> NullAndEmptyChecks.NotNull(value, paramName);
28+
=> NullAndEmptyChecks.Check(value, paramName);
2929

3030
/// <summary>
3131
/// Checks that the provided value is not <see langword="null" />.
@@ -52,7 +52,7 @@ public static TParamType OrExceptionWithMessage<TParamType>(
5252
[NotNull] string customMessage,
5353
[NotNull, CallerArgumentExpression(nameof(value))] string paramName = "")
5454
where TParamType : class
55-
=> NullAndEmptyChecks.NotNull(value, paramName, customMessage);
55+
=> NullAndEmptyChecks.Check(value, paramName, customMessage);
5656

5757
/// <summary>
5858
/// Checks that the provided value is not <see langword="null" />, empty (zero length), or contains white-space
@@ -123,7 +123,7 @@ public static string NotEmptyOrException(
123123
public static Guid NotEmptyOrException(Guid value,
124124
[NotNull, CallerArgumentExpression(nameof(value))] string paramName = "")
125125
{
126-
string validParamName = paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly();
126+
string validParamName = paramName.CheckNotZeroLengthOrWhiteSpaceOnly();
127127

128128
return IsEmpty(value) ? throw new ArgumentException(validParamName) : value;
129129
}
@@ -168,8 +168,8 @@ public static Guid NotEmptyOrExceptionWithMessage(Guid value,
168168
[NotNull, CallerArgumentExpression(nameof(value))] string paramName = "")
169169
{
170170
(string validParamName, string validCustomMessage) =
171-
(paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(),
172-
customMessage.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly());
171+
(paramName.CheckNotZeroLengthOrWhiteSpaceOnly(),
172+
customMessage.CheckNotZeroLengthOrWhiteSpaceOnly());
173173

174174
return IsEmpty(value) ?
175175
throw new ArgumentException(paramName: validParamName, message: validCustomMessage)
@@ -480,10 +480,10 @@ public static string ValidLuhnChecksum([NotNull] string? value, [NotNull] string
480480
[NotNull, CallerArgumentExpression(nameof(value))] string paramName = "")
481481
{
482482
(string validParamName, string validCustomMessage) =
483-
(paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(),
484-
customMessage.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly());
483+
(paramName.CheckNotZeroLengthOrWhiteSpaceOnly(),
484+
customMessage.CheckNotZeroLengthOrWhiteSpaceOnly());
485485

486-
string notNullValue = NullAndEmptyChecks.NotNull(value, validParamName);
486+
string notNullValue = NullAndEmptyChecks.Check(value, validParamName);
487487

488488
ValidateLuhnSequenceFormat(notNullValue, validCustomMessage);
489489

@@ -522,9 +522,9 @@ private static int[] ToDigitsArray(string notNullValue)
522522
public static string ValidBase64([NotNull] string? value, [NotNull, CallerArgumentExpression(nameof(value))] string paramName = "")
523523
{
524524
string validParamName =
525-
paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly();
525+
paramName.CheckNotZeroLengthOrWhiteSpaceOnly();
526526

527-
string notNullValue = value.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(validParamName);
527+
string notNullValue = value.CheckNotZeroLengthOrWhiteSpaceOnly(validParamName);
528528

529529
return IsBase64String(notNullValue) ? notNullValue
530530
: throw new FormatException($"{validParamName} is not a valid Base64 String.");
@@ -544,8 +544,8 @@ public static string ValidBase64([NotNull] string? value, [NotNull] string custo
544544
[NotNull, CallerArgumentExpression(nameof(value))] string paramName = "")
545545
{
546546
(string validParamName, string validCustomMessage) =
547-
(paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(),
548-
customMessage.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly());
547+
(paramName.CheckNotZeroLengthOrWhiteSpaceOnly(),
548+
customMessage.CheckNotZeroLengthOrWhiteSpaceOnly());
549549

550550
string notNullValue
551551
= value.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(validParamName, validCustomMessage);
@@ -607,11 +607,11 @@ private static TNullable CompliesWithExpected<TNullable>(
607607
[NotNull] string paramName,
608608
bool expected) where TNullable : class
609609
{
610-
TNullable notNullValue = value.ValueOrThrowIfNull(nameof(value));
611-
Func<TNullable, bool> notNullValidator = validator.ValueOrThrowIfNull();
612-
string notNullParamName = paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly();
610+
TNullable notNullValue = value.Check(nameof(value));
611+
Func<TNullable, bool> notNullValidator = validator.Check();
612+
string notNullParamName = paramName.CheckNotZeroLengthOrWhiteSpaceOnly();
613613
string notNullPreconditionDescription
614-
= preconditionDescription.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly();
614+
= preconditionDescription.CheckNotZeroLengthOrWhiteSpaceOnly();
615615

616616
return notNullValidator(notNullValue) != expected
617617
? throw new ArgumentException(paramName: notNullParamName, message: notNullPreconditionDescription)

src/Validations/ArgumentsHelpers/Extensions.cs

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,64 @@ namespace Triplex.Validations.ArgumentsHelpers;
66
internal 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
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
namespace Triplex.Validations.ArgumentsHelpers;
22

3+
/// <summary>
4+
/// Provides methods to validate that parameters are not null, empty, or whitespace.
5+
/// These methods throw exceptions if the checks fail, ensuring that the parameters meet the expected criteria.
6+
/// This class is intended for internal use within the Triplex library and should not be used directly by consumers.
7+
/// </summary>
8+
/// <remarks>
9+
/// As a general design principle, all checks begin with a <see langword="null"/> check.
10+
/// </remarks>
311
internal static class NullAndEmptyChecks
412
{
513
[return: NotNull]
6-
internal static TParamType NotNull<TParamType>([NotNull] TParamType? value,
7-
[NotNull] string paramName) where TParamType : class
8-
=> value.ValueOrThrowIfNull(paramName.ValueOrThrowIfNull());
14+
internal static TParamType Check<TParamType>([NotNull] TParamType? value, [NotNull] string paramName)
15+
where TParamType : class
16+
=> value.CheckWithParamName(paramName.CheckParamName());
917

1018
[return: NotNull]
11-
internal static TParamType NotNull<TParamType>([NotNull] TParamType? value,
12-
[NotNull] string paramName, [NotNull] string customMessage)
19+
internal static TParamType Check<TParamType>([NotNull] TParamType? value, [NotNull] string paramName, [NotNull] string customMessage)
1320
where TParamType : class
14-
=> value.ValueOrThrowIfNull(paramName.ValueOrThrowIfNull(),
15-
customMessage.ValueOrThrowIfNull());
21+
=> value.CheckWithParamName(paramName.CheckParamName(), customMessage.CheckExceptionMessage());
1622

1723
[return: NotNull]
18-
internal static string NotNullEmptyOrWhiteSpaceOnly([NotNull] string? value,
19-
[NotNull] string paramName)
20-
=> NotNullOrEmpty(value, paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly())
21-
.ValueOrThrowIfWhiteSpaceOnly(paramName);
24+
internal static string NotNullEmptyOrWhiteSpaceOnly([NotNull] string? value, [NotNull] string paramName)
25+
=> NotNullOrEmpty(value, paramName.CheckParamName())
26+
.CheckNotWhiteSpaceOnly(paramName);
2227

2328
[return: NotNull]
2429
internal static string NotNullEmptyOrWhiteSpaceOnly([NotNull] string? value,
2530
[NotNull] string paramName, [NotNull] string customMessage)
2631
=> NotNullOrEmpty(value, paramName, customMessage)
27-
.ValueOrThrowIfWhiteSpaceOnly(paramName, customMessage);
32+
.CheckNotWhiteSpaceOnly(paramName, customMessage);
2833

2934
[return: NotNull]
30-
internal static string NotNullOrEmpty([NotNull] string? value,
31-
[NotNull] string paramName)
32-
=> value.ValueOrThrowIfNullOrZeroLength(
33-
paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly());
35+
internal static string NotNullOrEmpty([NotNull] string? value, [NotNull] string paramName)
36+
=> value.CheckNotZeroLength(
37+
paramName.CheckParamName());
3438

3539
[return: NotNull]
3640
internal static string NotNullOrEmpty([NotNull] string? value,
3741
[NotNull] string paramName, [NotNull] string customMessage)
38-
=> value.ValueOrThrowIfNullOrZeroLength(
39-
paramName.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly(),
40-
customMessage.ValueOrThrowIfNullZeroLengthOrWhiteSpaceOnly());
42+
=> value.CheckNotZeroLength(
43+
paramName.CheckParamName(),
44+
customMessage.CheckExceptionMessage());
4145
}

0 commit comments

Comments
 (0)