| | | 1 | | #region Usings |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | #endregion |
| | | 4 | | |
| | | 5 | | namespace ZhEaIsNsAaBn.Utilities.ValidationResponses; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a generic validation response that extends <see cref="ValidationResponse" /> to include typed data. |
| | | 9 | | /// Provides validation functionality along with the ability to carry validated data of type T. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="T">The type of data being validated and carried in the response.</typeparam> |
| | | 12 | | public class ValidationResponse< T > : ValidationResponse |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets or sets the data row or object being validated. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <value>The data of type T that was validated; <see langword="default"/> when created from errors only.</value> |
| | | 18 | | public T? Row { get; set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Creates a new <see cref="ValidationResponse{T}" /> from an existing <see cref="ValidationResponse" />, |
| | | 22 | | /// copying the validation errors but without setting the Row property. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="validation">The validation response to copy errors from.</param> |
| | | 25 | | /// <returns>A new <see cref="ValidationResponse{T}" /> instance with the same errors.</returns> |
| | | 26 | | [ SuppressMessage( "Design", "CA1000:Do not declare static members on generic types", |
| | | 27 | | Justification = "Factory on the generic response, same house pattern as LayerResponse<T> (STYLE_G |
| | | 28 | | public static ValidationResponse<T> FromValidation( ValidationResponse validation ) => |
| | 3 | 29 | | new() |
| | 3 | 30 | | { |
| | 3 | 31 | | Errors = validation.Errors |
| | 3 | 32 | | }; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Implicitly converts a value of type T to a <see cref="ValidationResponse{T}" /> with no validation errors. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="value">The value to convert.</param> |
| | | 38 | | /// <returns>A new <see cref="ValidationResponse{T}" /> instance containing the specified value with no errors.</ret |
| | | 39 | | public static implicit operator ValidationResponse<T>( T value ) => |
| | 12 | 40 | | new() |
| | 12 | 41 | | { |
| | 12 | 42 | | Row = value |
| | 12 | 43 | | }; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Implicitly converts a list of validation errors to a <see cref="ValidationResponse{T}" />. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="errors">The list of validation errors to convert.</param> |
| | | 49 | | /// <returns>A new <see cref="ValidationResponse{T}" /> instance containing the specified errors.</returns> |
| | | 50 | | public static implicit operator ValidationResponse<T>( List<ColumnValidationError> errors ) => |
| | 0 | 51 | | new() |
| | 0 | 52 | | { |
| | 0 | 53 | | Errors = errors |
| | 0 | 54 | | }; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Implicitly converts an array of validation errors to a <see cref="ValidationResponse{T}" />. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="errors">The array of validation errors to convert.</param> |
| | | 60 | | /// <returns>A new <see cref="ValidationResponse{T}" /> instance containing the specified errors.</returns> |
| | | 61 | | public static implicit operator ValidationResponse<T>( ColumnValidationError[] errors ) => |
| | 0 | 62 | | new() |
| | 0 | 63 | | { |
| | 0 | 64 | | Errors = errors.ToList() |
| | 0 | 65 | | }; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Implicitly converts a single validation error to a <see cref="ValidationResponse{T}" />. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="errors">The validation error to convert.</param> |
| | | 71 | | /// <returns>A new <see cref="ValidationResponse{T}" /> instance containing the specified error.</returns> |
| | | 72 | | public static implicit operator ValidationResponse<T>( ColumnValidationError errors ) => |
| | 6 | 73 | | new() |
| | 6 | 74 | | { |
| | 6 | 75 | | Errors = [errors] |
| | 6 | 76 | | }; |
| | | 77 | | } |