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
38 lines (35 loc) · 1.46 KB
/
ValidationResultExtensions.cs
File metadata and controls
38 lines (35 loc) · 1.46 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
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Validation;
using ServiceStack.FluentValidation.Results;
using ServiceStack.ServiceInterface.ServiceModel;
namespace ServiceStack.ServiceInterface.Validation
{
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));
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());
}
}
}