-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimitiveValidationResult.cs
51 lines (44 loc) · 2.14 KB
/
PrimitiveValidationResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Diagnostics.CodeAnalysis;
namespace AltaSoft.DomainPrimitives;
/// <summary>
/// DomainPrimitive validation result.
/// </summary>
public readonly struct PrimitiveValidationResult
{
/// <summary>
/// Gets a value indicating whether the validation result is valid.
/// </summary>
[MemberNotNullWhen(false, nameof(ErrorMessage))]
public bool IsValid { get; }
/// <summary>
/// Gets the value associated with the validation result.
/// </summary>
public string? ErrorMessage { get; }
/// <summary>
/// Creates a new instance of the <see cref="PrimitiveValidationResult"/> struct with a valid result.
/// </summary>
/// <returns>A new instance of the <see cref="PrimitiveValidationResult"/> struct with a valid result.</returns>
public static readonly PrimitiveValidationResult Ok = new(true, null);
/// <summary>
/// Creates a new instance of the <see cref="PrimitiveValidationResult"/> struct with an error result.
/// </summary>
/// <param name="error">The error message associated with the result.</param>
/// <returns>A new instance of the <see cref="PrimitiveValidationResult"/> struct with an error result.</returns>
public static PrimitiveValidationResult Error(string error) => new(false, error);
/// <summary>
/// Implicitly converts a string value to a <see cref="PrimitiveValidationResult"/> with an error result.
/// </summary>
/// <param name="value">The string value representing the error message.</param>
/// <returns>A <see cref="PrimitiveValidationResult"/> with an error result.</returns>
public static implicit operator PrimitiveValidationResult(string value) => Error(value);
/// <summary>
/// Initializes a new instance of the <see cref="PrimitiveValidationResult"/> struct.
/// </summary>
/// <param name="isValid">A value indicating whether the validation result is valid.</param>
/// <param name="errorMessage">The value associated with the validation result.</param>
private PrimitiveValidationResult(bool isValid, string? errorMessage)
{
IsValid = isValid;
ErrorMessage = errorMessage;
}
}