-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from deveel/validation-error-handling
Improvements to the Validation Error Handling
- Loading branch information
Showing
8 changed files
with
221 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace Deveel | ||
{ | ||
/// <summary> | ||
/// Extensions for the <see cref="IValidationError"/> contract. | ||
/// </summary> | ||
public static class ValidationErrorExtensions | ||
{ | ||
/// <summary> | ||
/// Gets a dictionary of member names and the list of error messages | ||
/// </summary> | ||
/// <param name="error"> | ||
/// The validation error to get the member errors from. | ||
/// </param> | ||
/// <returns> | ||
/// Returns a dictionary where the key is the member name and the value | ||
/// is the list of error messages for that member. | ||
/// </returns> | ||
public static IDictionary<string, string[]> GetMemberErrors(this IValidationError error) | ||
{ | ||
ArgumentNullException.ThrowIfNull(error, nameof(error)); | ||
|
||
var results = new Dictionary<string, List<string>>(); | ||
|
||
foreach (var result in error.ValidationResults) | ||
{ | ||
foreach (var memberName in result.MemberNames) | ||
{ | ||
if (String.IsNullOrWhiteSpace(result.ErrorMessage)) | ||
continue; | ||
|
||
if (!results.TryGetValue(memberName, out var messages)) | ||
{ | ||
messages = new List<string>(); | ||
results[memberName] = messages; | ||
} | ||
|
||
messages.Add(result.ErrorMessage); | ||
} | ||
} | ||
|
||
return results.ToDictionary(x => x.Key, x => x.Value.ToArray()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters