| | | 1 | | using FluentValidation.Results; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.FluentValidation; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Converts FluentValidation <see cref="ValidationFailure"/> instances to Needlr <see cref="ValidationError"/> instance |
| | | 9 | | /// </summary> |
| | | 10 | | public static class ValidationFailureConverter |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Converts a FluentValidation <see cref="ValidationFailure"/> to a Needlr <see cref="ValidationError"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="failure">The FluentValidation failure to convert.</param> |
| | | 16 | | /// <returns>A Needlr <see cref="ValidationError"/> with the same information.</returns> |
| | | 17 | | public static ValidationError ToValidationError(this ValidationFailure failure) |
| | | 18 | | { |
| | 4 | 19 | | ArgumentNullException.ThrowIfNull(failure); |
| | | 20 | | |
| | 4 | 21 | | return new ValidationError(failure.ErrorMessage) |
| | 4 | 22 | | { |
| | 4 | 23 | | PropertyName = failure.PropertyName, |
| | 4 | 24 | | ErrorCode = failure.ErrorCode, |
| | 4 | 25 | | Severity = ConvertSeverity(failure.Severity) |
| | 4 | 26 | | }; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Converts a collection of FluentValidation failures to Needlr validation errors. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="failures">The FluentValidation failures to convert.</param> |
| | | 33 | | /// <returns>An enumerable of Needlr <see cref="ValidationError"/> instances.</returns> |
| | | 34 | | public static IEnumerable<ValidationError> ToValidationErrors(this IEnumerable<ValidationFailure> failures) |
| | | 35 | | { |
| | 2 | 36 | | ArgumentNullException.ThrowIfNull(failures); |
| | | 37 | | |
| | 5 | 38 | | return failures.Select(f => f.ToValidationError()); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Converts a FluentValidation <see cref="ValidationResult"/> to a collection of Needlr validation errors. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="result">The FluentValidation result to convert.</param> |
| | | 45 | | /// <returns>An enumerable of Needlr <see cref="ValidationError"/> instances.</returns> |
| | | 46 | | public static IEnumerable<ValidationError> ToValidationErrors(this ValidationResult result) |
| | | 47 | | { |
| | 1 | 48 | | ArgumentNullException.ThrowIfNull(result); |
| | | 49 | | |
| | 1 | 50 | | return result.Errors.ToValidationErrors(); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private static ValidationSeverity ConvertSeverity(global::FluentValidation.Severity severity) |
| | | 54 | | { |
| | 4 | 55 | | return severity switch |
| | 4 | 56 | | { |
| | 3 | 57 | | global::FluentValidation.Severity.Error => ValidationSeverity.Error, |
| | 1 | 58 | | global::FluentValidation.Severity.Warning => ValidationSeverity.Warning, |
| | 0 | 59 | | global::FluentValidation.Severity.Info => ValidationSeverity.Info, |
| | 0 | 60 | | _ => ValidationSeverity.Error |
| | 4 | 61 | | }; |
| | | 62 | | } |
| | | 63 | | } |