< Summary

Information
Class: NexusLabs.Needlr.Generators.ValidationError
Assembly: NexusLabs.Needlr.Generators.Attributes
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Generators.Attributes/ValidationError.cs
Line coverage
42%
Covered lines: 9
Uncovered lines: 12
Coverable lines: 21
Total lines: 134
Line coverage: 42.8%
Branch coverage
11%
Covered branches: 2
Total branches: 18
Branch coverage: 11.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
get_Message()100%11100%
get_PropertyName()100%11100%
get_ErrorCode()100%11100%
get_Severity()100%11100%
op_Implicit(...)100%11100%
ToString()50%22100%
Equals(...)0%7280%
GetHashCode()0%4260%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.Generators.Attributes/ValidationError.cs

#LineLine coverage
 1// Copyright (c) NexusLabs. All rights reserved.
 2// Licensed under the MIT License.
 3
 4namespace NexusLabs.Needlr.Generators;
 5
 6/// <summary>
 7/// Represents a validation error with optional structured information.
 8/// </summary>
 9/// <remarks>
 10/// <para>
 11/// This class provides rich validation error information including the property name,
 12/// error code, and severity level. For simple cases, strings can be implicitly converted
 13/// to <see cref="ValidationError"/> via the implicit operator.
 14/// </para>
 15/// </remarks>
 16/// <example>
 17/// <code>
 18/// // Simple: just yield a string (implicit conversion)
 19/// yield return "Name is required";
 20///
 21/// // Rich: provide structured information
 22/// yield return new ValidationError("API key format is invalid")
 23/// {
 24///     PropertyName = nameof(ApiKey),
 25///     ErrorCode = "API_KEY_FORMAT",
 26///     Severity = ValidationSeverity.Error
 27/// };
 28///
 29/// // Warning (won't fail startup)
 30/// yield return new ValidationError("Timeout is unusually high")
 31/// {
 32///     PropertyName = nameof(Timeout),
 33///     Severity = ValidationSeverity.Warning
 34/// };
 35/// </code>
 36/// </example>
 37public sealed class ValidationError
 38{
 39    /// <summary>
 40    /// Initializes a new instance of <see cref="ValidationError"/> with the specified message.
 41    /// </summary>
 42    /// <param name="message">The error message.</param>
 643    public ValidationError(string message)
 44    {
 645        Message = message ?? throw new System.ArgumentNullException(nameof(message));
 646    }
 47
 48    /// <summary>
 49    /// Gets the error message.
 50    /// </summary>
 451    public string Message { get; }
 52
 53    /// <summary>
 54    /// Gets or sets the name of the property that failed validation, if applicable.
 55    /// </summary>
 956    public string? PropertyName { get; set; }
 57
 58    /// <summary>
 59    /// Gets or sets an error code for programmatic handling or localization.
 60    /// </summary>
 561    public string? ErrorCode { get; set; }
 62
 63    /// <summary>
 64    /// Gets or sets the severity of the validation error.
 65    /// Only <see cref="ValidationSeverity.Error"/> will cause startup to fail.
 66    /// </summary>
 567    public ValidationSeverity Severity { get; set; } = ValidationSeverity.Error;
 68
 69    /// <summary>
 70    /// Implicitly converts a string to a <see cref="ValidationError"/>.
 71    /// </summary>
 72    /// <param name="message">The error message.</param>
 73    /// <returns>A new <see cref="ValidationError"/> with the specified message.</returns>
 74    public static implicit operator ValidationError(string message)
 275        => new ValidationError(message);
 76
 77    /// <summary>
 78    /// Returns the error message, optionally prefixed with the property name.
 79    /// </summary>
 80    public override string ToString()
 281        => PropertyName != null ? $"{PropertyName}: {Message}" : Message;
 82
 83    /// <summary>
 84    /// Determines whether the specified object is equal to the current object.
 85    /// </summary>
 86    public override bool Equals(object? obj)
 87    {
 088        if (obj is ValidationError other)
 89        {
 090            return Message == other.Message &&
 091                   PropertyName == other.PropertyName &&
 092                   ErrorCode == other.ErrorCode &&
 093                   Severity == other.Severity;
 94        }
 095        return false;
 96    }
 97
 98    /// <summary>
 99    /// Returns the hash code for this validation error.
 100    /// </summary>
 101    public override int GetHashCode()
 102    {
 103        unchecked
 104        {
 0105            var hash = 17;
 0106            hash = hash * 23 + (Message?.GetHashCode() ?? 0);
 0107            hash = hash * 23 + (PropertyName?.GetHashCode() ?? 0);
 0108            hash = hash * 23 + (ErrorCode?.GetHashCode() ?? 0);
 0109            hash = hash * 23 + Severity.GetHashCode();
 0110            return hash;
 111        }
 112    }
 113}
 114
 115/// <summary>
 116/// Specifies the severity of a validation error.
 117/// </summary>
 118public enum ValidationSeverity
 119{
 120    /// <summary>
 121    /// An error that will cause validation to fail and prevent startup.
 122    /// </summary>
 123    Error,
 124
 125    /// <summary>
 126    /// A warning that will be logged but won't prevent startup.
 127    /// </summary>
 128    Warning,
 129
 130    /// <summary>
 131    /// An informational message that will be logged.
 132    /// </summary>
 133    Info
 134}