< Summary

Information
Class: NexusLabs.Needlr.FluentValidation.ValidationFailureConverter
Assembly: NexusLabs.Needlr.FluentValidation
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.FluentValidation/ValidationFailureConverter.cs
Line coverage
88%
Covered lines: 16
Uncovered lines: 2
Coverable lines: 18
Total lines: 63
Line coverage: 88.8%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ToValidationError(...)100%11100%
ToValidationErrors(...)100%11100%
ToValidationErrors(...)100%11100%
ConvertSeverity(...)50%4471.42%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.FluentValidation/ValidationFailureConverter.cs

#LineLine coverage
 1using FluentValidation.Results;
 2
 3using NexusLabs.Needlr.Generators;
 4
 5namespace NexusLabs.Needlr.FluentValidation;
 6
 7/// <summary>
 8/// Converts FluentValidation <see cref="ValidationFailure"/> instances to Needlr <see cref="ValidationError"/> instance
 9/// </summary>
 10public 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    {
 419        ArgumentNullException.ThrowIfNull(failure);
 20
 421        return new ValidationError(failure.ErrorMessage)
 422        {
 423            PropertyName = failure.PropertyName,
 424            ErrorCode = failure.ErrorCode,
 425            Severity = ConvertSeverity(failure.Severity)
 426        };
 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    {
 236        ArgumentNullException.ThrowIfNull(failures);
 37
 538        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    {
 148        ArgumentNullException.ThrowIfNull(result);
 49
 150        return result.Errors.ToValidationErrors();
 51    }
 52
 53    private static ValidationSeverity ConvertSeverity(global::FluentValidation.Severity severity)
 54    {
 455        return severity switch
 456        {
 357            global::FluentValidation.Severity.Error => ValidationSeverity.Error,
 158            global::FluentValidation.Severity.Warning => ValidationSeverity.Warning,
 059            global::FluentValidation.Severity.Info => ValidationSeverity.Info,
 060            _ => ValidationSeverity.Error
 461        };
 62    }
 63}