|
1 | 1 | using NodaMoney; |
| 2 | +using System.ComponentModel.DataAnnotations; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Globalization; |
2 | 5 | using System.Text.Json; |
3 | 6 |
|
4 | 7 | namespace NodaMoneyTest; |
5 | 8 |
|
6 | | -internal static class SerializationTest |
| 9 | +public static class SerializationTest |
7 | 10 | { |
8 | 11 |
|
9 | 12 | #region Constants & Statics |
10 | 13 |
|
| 14 | + public static void Input_Validation_Test() |
| 15 | + { |
| 16 | + var input = new MyInput("Jane Doe", 25, new FastMoneyDto { Amount = 49.95m, }); |
| 17 | + var json = JsonSerializer.Serialize(input); |
| 18 | + Console.WriteLine(json); |
| 19 | + } |
| 20 | + |
| 21 | + public static void Output_Serialization_Test() |
| 22 | + { |
| 23 | + var money = new FastMoney(99.99m); |
| 24 | + var dto = money.ToDto(); |
| 25 | + |
| 26 | + var output = new MyOutput("Alice Smith", 28, dto); |
| 27 | + var json = JsonSerializer.Serialize(output); |
| 28 | + Console.WriteLine(json); |
| 29 | + } |
| 30 | + |
11 | 31 | public static void Serialize_Test() |
12 | 32 | { |
13 | 33 | var dto = new MyDto("John Doe", 30, new FastMoney(99.99m, "USD")); |
14 | 34 | var json = JsonSerializer.Serialize(dto); |
15 | 35 |
|
16 | 36 | Console.WriteLine(json); |
17 | | - |
18 | | - var deserializedDto = JsonSerializer.Deserialize<MyDto>(json); |
| 37 | + _ = JsonSerializer.Deserialize<MyDto>(json); |
19 | 38 | } |
20 | 39 |
|
21 | 40 | #endregion |
22 | 41 |
|
23 | 42 | } |
24 | 43 |
|
25 | 44 | public record MyDto(string Name, int Age, FastMoney Price); |
| 45 | + |
| 46 | +public record MyInput(string Name, int Age, [AmountValidation("1.00000", "100")] FastMoneyDto Price); |
| 47 | + |
| 48 | +public record MyOutput(string Name, int Age, FastMoneyDto Price); |
| 49 | + |
| 50 | +public static class FastMoneyExtensions |
| 51 | +{ |
| 52 | + |
| 53 | + #region Constants & Statics |
| 54 | + |
| 55 | + public static FastMoneyDto ToDto(this FastMoney fastMoney) |
| 56 | + { |
| 57 | + return new FastMoneyDto { Amount = fastMoney.Amount, Currency = fastMoney.Currency.Code }; |
| 58 | + } |
| 59 | + |
| 60 | + #endregion |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +public class FastMoneyDto : IValidatableObject |
| 65 | +{ |
| 66 | + |
| 67 | + #region Properties |
| 68 | + |
| 69 | + public decimal Amount { get; init; } |
| 70 | + |
| 71 | + public string Currency { get; init; } = CurrencyCode.CNY; |
| 72 | + |
| 73 | + #endregion |
| 74 | + |
| 75 | + #region IValidatableObject implementations |
| 76 | + |
| 77 | + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) |
| 78 | + { |
| 79 | + var results = new List<ValidationResult>(); |
| 80 | + |
| 81 | + //validate AmountValidationAttribute |
| 82 | + var context = new ValidationContext(this); |
| 83 | + _ = Validator.TryValidateProperty(Amount, context, results); |
| 84 | + |
| 85 | + try |
| 86 | + { |
| 87 | + _ = CurrencyInfo.FromCode(Currency); |
| 88 | + } |
| 89 | + catch |
| 90 | + { |
| 91 | + results.Add(new ValidationResult("Invalid currency code.", [nameof(Currency)])); |
| 92 | + } |
| 93 | + |
| 94 | + return results; |
| 95 | + } |
| 96 | + |
| 97 | + #endregion |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] |
| 102 | +public sealed class AmountValidationAttribute : ValidationAttribute |
| 103 | +{ |
| 104 | + [SuppressMessage("Design", "CA1019:Define accessors for attribute arguments", Justification = "<Pending>")] |
| 105 | + public AmountValidationAttribute(string minimum, string maximum) |
| 106 | + { |
| 107 | + Minimum = decimal.Parse(minimum, CultureInfo.InvariantCulture); |
| 108 | + Maximum = decimal.Parse(maximum, CultureInfo.InvariantCulture); |
| 109 | + |
| 110 | + ArgumentOutOfRangeException.ThrowIfLessThan(Minimum, FastMoney.MinValue.Amount); |
| 111 | + ArgumentOutOfRangeException.ThrowIfGreaterThan(Maximum, FastMoney.MaxValue.Amount); |
| 112 | + |
| 113 | + if (Minimum.Scale > 4) |
| 114 | + { |
| 115 | + throw new ArgumentException("Scale cannot be greater than 4.", nameof(minimum)); |
| 116 | + } |
| 117 | + if (Maximum.Scale > 4) |
| 118 | + { |
| 119 | + throw new ArgumentException("Scale cannot be greater than 4.", nameof(maximum)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + #region Properties |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Gets the maximum allowed field value. |
| 127 | + /// </summary> |
| 128 | + public decimal Maximum { get; } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Specifies whether validation should fail for values that are equal to System.ComponentModel.DataAnnotations.RangeAttribute.Maximum. |
| 132 | + /// </summary> |
| 133 | + public bool MaximumIsExclusive { get; set; } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Gets the minimum allowed field value. |
| 137 | + /// </summary> |
| 138 | + public decimal Minimum { get; } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Specifies whether validation should fail for values that are equal to System.ComponentModel.DataAnnotations.RangeAttribute.Minimum. |
| 142 | + /// </summary> |
| 143 | + public bool MinimumIsExclusive { get; set; } |
| 144 | + |
| 145 | + #endregion |
| 146 | + |
| 147 | +} |
0 commit comments