| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | namespace 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> |
| | | 37 | | public 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> |
| | 6 | 43 | | public ValidationError(string message) |
| | | 44 | | { |
| | 6 | 45 | | Message = message ?? throw new System.ArgumentNullException(nameof(message)); |
| | 6 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the error message. |
| | | 50 | | /// </summary> |
| | 4 | 51 | | public string Message { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets or sets the name of the property that failed validation, if applicable. |
| | | 55 | | /// </summary> |
| | 9 | 56 | | public string? PropertyName { get; set; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets or sets an error code for programmatic handling or localization. |
| | | 60 | | /// </summary> |
| | 5 | 61 | | 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> |
| | 5 | 67 | | 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) |
| | 2 | 75 | | => new ValidationError(message); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Returns the error message, optionally prefixed with the property name. |
| | | 79 | | /// </summary> |
| | | 80 | | public override string ToString() |
| | 2 | 81 | | => 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 | | { |
| | 0 | 88 | | if (obj is ValidationError other) |
| | | 89 | | { |
| | 0 | 90 | | return Message == other.Message && |
| | 0 | 91 | | PropertyName == other.PropertyName && |
| | 0 | 92 | | ErrorCode == other.ErrorCode && |
| | 0 | 93 | | Severity == other.Severity; |
| | | 94 | | } |
| | 0 | 95 | | 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 | | { |
| | 0 | 105 | | var hash = 17; |
| | 0 | 106 | | hash = hash * 23 + (Message?.GetHashCode() ?? 0); |
| | 0 | 107 | | hash = hash * 23 + (PropertyName?.GetHashCode() ?? 0); |
| | 0 | 108 | | hash = hash * 23 + (ErrorCode?.GetHashCode() ?? 0); |
| | 0 | 109 | | hash = hash * 23 + Severity.GetHashCode(); |
| | 0 | 110 | | return hash; |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Specifies the severity of a validation error. |
| | | 117 | | /// </summary> |
| | | 118 | | public 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 | | } |