Skip to content

Commit 81170ce

Browse files
added validate messages
1 parent 92e8e13 commit 81170ce

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

src/OneBitSoftware.Utilities.OperationResult/OneBitSoftware.Utilities.OperationResult.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
3333
<PackageReadmeFile>README.md</PackageReadmeFile>
3434
<PackageTags>OneBitSoftware; OperationResult;</PackageTags>
35-
<Version>1.2.1</Version>
35+
<Version>1.3.4</Version>
3636
</PropertyGroup>
3737

3838
</Project>

src/OneBitSoftware.Utilities.OperationResult/OperationResult.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,20 @@ public OperationResult<TResult> AppendErrorMessages<TOther>(TOther otherOperatio
329329

330330
return this;
331331
}
332+
333+
/// <summary>
334+
/// Sets related object to the provided operation result and returns itself.
335+
/// </summary>
336+
/// <param name="operationResult">The <see cref="OperationResult{TResult}"/> instance to extend.</param>
337+
/// <param name="relatedObject">The value to set to the <see cref="OperationResult{TResult}.ResultObject"/> property.</param>
338+
/// <returns>Returns the original <paramref name="operationResult"/> with the appended message.</returns>
339+
/// <remarks>
340+
/// This method will throw if the provided <paramref name="operationResult"/> is null.
341+
/// </remarks>
342+
public OperationResult<TResult> WithRelatedObject(TResult relatedObject)
343+
{
344+
this.ResultObject = relatedObject;
345+
return this;
346+
}
332347
}
333348
}

src/OneBitSoftware.Utilities.OperationResult/OperationResultValidationExtensions.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ public static void ValidateAny<T, TValue>(this OperationResult<T> operationResul
3636
}
3737
}
3838

39+
/// <summary>
40+
/// Use this method to check if a value is not null and not an empty collection.
41+
/// If <paramref name="value"/> is null or an empty collection, an error message should be appended and a log of the passed <paramref name="level"/> severity should be created.
42+
/// </summary>
43+
/// <typeparam name="TValue">The type of the underlying entities that are stored within the requested collection.</typeparam>
44+
/// <param name="operationResult">The <see cref="OperationResult"/> instance.</param>
45+
/// <param name="value">The collection that should be validated.</param>
46+
/// <param name="className">The name of the class where the <paramref name="methodName"/> is defined.</param>
47+
/// <param name="methodName">The name of the method where <paramref name="value"/> is used.</param>
48+
/// <param name="identifierPropertyName">The name of the entity's unique identifier property.</param>
49+
/// <param name="level">The logging severity.</param>
50+
public static void ValidateAny<TValue>(this OperationResult operationResult, IEnumerable<TValue> value, string className, string methodName, string identifierPropertyName, LogLevel level = LogLevel.Error)
51+
{
52+
// If the passed value is null, log and append an error message.
53+
if (value == null)
54+
{
55+
var errorMessage = $"{className}, {methodName} - An entity with that {identifierPropertyName} does not exist.";
56+
operationResult.AppendError(errorMessage, logLevel: level);
57+
}
58+
59+
// If the passed value is an empty collection, log and append an error message.
60+
else if (!value.Any())
61+
{
62+
var errorMessage = $"{className}, {methodName} - The collection with that {identifierPropertyName} is empty which is not permitted.";
63+
operationResult.AppendError(errorMessage, logLevel: level);
64+
}
65+
}
66+
3967
/// <summary>
4068
/// Use this method to check if a value is equal to its default value.
4169
/// If <paramref name="value"/> is equal to its default value, an error message should be appended and a log of the passed <paramref name="level"/> severity would be created.
@@ -57,6 +85,26 @@ public static void ValidateDefault<T, TValue>(this OperationResult<T> operationR
5785
operationResult.AppendError(errorMessage, logLevel: level);
5886
}
5987

88+
/// <summary>
89+
/// Use this method to check if a value is equal to its default value.
90+
/// If <paramref name="value"/> is equal to its default value, an error message should be appended and a log of the passed <paramref name="level"/> severity would be created.
91+
/// </summary>
92+
/// <typeparam name="TValue">The type of the <paramref name="value"/>.</typeparam>
93+
/// <param name="value">The value that should be validated.</param>
94+
/// <param name="className">The name of the class where the <paramref name="methodName"/> is defined.</param>
95+
/// <param name="methodName">The name of he method where <paramref name="value"/> is used.</param>
96+
/// <param name="propertyName">The name of the property.</param>
97+
/// <param name="level">The logging severity.</param>
98+
public static void ValidateDefault<TValue>(this OperationResult operationResult, TValue value, string className, string methodName, string propertyName, LogLevel level = LogLevel.Error)
99+
where TValue : struct, IEquatable<TValue>
100+
{
101+
// If the passed value is null, log and append an error message.
102+
if (value.Equals(default) == false) return;
103+
104+
var errorMessage = $"{className}, {methodName} - The {propertyName} has a default value.";
105+
operationResult.AppendError(errorMessage, logLevel: level);
106+
}
107+
60108
/// <summary>
61109
/// Use this method to check if a value is a valid string.
62110
/// If <paramref name="value"/> is null, empty or consists only of whitespace characters, an error message should be appended and a log of the passed <paramref name="level"/> severity would be created.
@@ -76,6 +124,24 @@ public static void ValidateNullOrWhitespace<T>(this OperationResult<T> operation
76124
operationResult.AppendError(errorMessage, logLevel: level);
77125
}
78126

127+
/// <summary>
128+
/// Use this method to check if a value is a valid string.
129+
/// If <paramref name="value"/> is null, empty or consists only of whitespace characters, an error message should be appended and a log of the passed <paramref name="level"/> severity would be created.
130+
/// </summary>
131+
/// <param name="value">The value that should be validated.</param>
132+
/// <param name="className">The name of the class where the <paramref name="methodName"/> is defined.</param>
133+
/// <param name="methodName">The name of he method where <paramref name="value"/> is used.</param>
134+
/// <param name="propertyName">The name of the property.</param>
135+
/// <param name="level">The logging severity.</param>
136+
public static void ValidateNullOrWhitespace(this OperationResult operationResult, string value, string className, string methodName, string propertyName, LogLevel level = LogLevel.Error)
137+
{
138+
// If the passed value is null, empty or consists only of whitespace characters, log and append an error message.
139+
if (string.IsNullOrWhiteSpace(value) == false) return;
140+
141+
var errorMessage = $"{className}, {methodName} - The {propertyName} is null, empty or consists only of whitespace characters.";
142+
operationResult.AppendError(errorMessage, logLevel: level);
143+
}
144+
79145
/// <summary>
80146
/// Use this method to check if a value is not null.
81147
/// If you want to validate that an entity exists, use the "ValidateExist" extension method.

0 commit comments

Comments
 (0)