forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationResultExtensions.cs
More file actions
34 lines (30 loc) · 1.34 KB
/
ValidationResultExtensions.cs
File metadata and controls
34 lines (30 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using ServiceStack.FluentValidation.Results;
using ServiceStack.Validation;
namespace ServiceStack.ServiceInterface
{
public static class ValidationResultExtensions
{
/// <summary>
/// Converts the validation result to an error result which will be serialized by ServiceStack in a clean and human-readable way.
/// </summary>
/// <param name="result">The validation result</param>
/// <returns></returns>
public static ValidationErrorResult ToErrorResult(this ValidationResult result)
{
var validationResult = new ValidationErrorResult();
foreach (var error in result.Errors)
validationResult.Errors.Add(new ValidationErrorField(error.ErrorCode, error.PropertyName, error.ErrorMessage, error.AttemptedValue));
return validationResult;
}
/// <summary>
/// Converts the validation result to an error exception which will be serialized by ServiceStack in a clean and human-readable way
/// if the returned exception is thrown.
/// </summary>
/// <param name="result">The validation result</param>
/// <returns></returns>
public static ValidationError ToException(this ValidationResult result)
{
return new ValidationError(result.ToErrorResult());
}
}
}