|
9 | 9 | using AltaSoft.DomainPrimitives.Generator.Helpers; |
10 | 10 | using AltaSoft.DomainPrimitives.Generator.Models; |
11 | 11 | using Microsoft.CodeAnalysis; |
| 12 | +using Microsoft.CodeAnalysis.CSharp; |
12 | 13 |
|
13 | 14 | namespace AltaSoft.DomainPrimitives.Generator; |
14 | 15 |
|
@@ -389,6 +390,11 @@ private static void Process(GeneratorData data, string ctorCode, DomainPrimitive |
389 | 390 | usings.Add("System.Globalization"); |
390 | 391 | } |
391 | 392 |
|
| 393 | + if (data.ValidatePattern) |
| 394 | + { |
| 395 | + usings.Add("System.Text.RegularExpressions"); |
| 396 | + } |
| 397 | + |
392 | 398 | var needsMathOperators = data.GenerateAdditionOperators || data.GenerateDivisionOperators || |
393 | 399 | data.GenerateMultiplyOperators || data.GenerateSubtractionOperators || data.GenerateModulusOperator; |
394 | 400 |
|
@@ -780,7 +786,11 @@ private static bool ProcessConstructor(GeneratorData data, SourceCodeBuilder bui |
780 | 786 | .OpenBracket(); |
781 | 787 |
|
782 | 788 | if (data.UnderlyingType == DomainPrimitiveUnderlyingType.String) |
| 789 | + { |
783 | 790 | AddStringLengthAttributeValidation(type, data, builder); |
| 791 | + AddPatternAttribute(type, data, builder); |
| 792 | + |
| 793 | + } |
784 | 794 |
|
785 | 795 | builder.AppendLine("ValidateOrThrow(value);"); |
786 | 796 | builder.CloseBracket() |
@@ -836,4 +846,33 @@ private static void AddStringLengthAttributeValidation(ISymbol domainPrimitiveTy |
836 | 846 | .AppendLine($"\tthrow InvalidDomainValueException.StringRangeException(typeof({data.ClassName}), value, {minValue.ToString(CultureInfo.InvariantCulture)}, {maxValue.ToString(CultureInfo.InvariantCulture)});") |
837 | 847 | .NewLine(); |
838 | 848 | } |
| 849 | + |
| 850 | + /// <summary> |
| 851 | + /// Adds pattern validation to the constructor if the Domain Primitive type is decorated with the PatternAttribute. |
| 852 | + /// </summary> |
| 853 | + private static void AddPatternAttribute(ISymbol domainPrimitiveType, GeneratorData data, SourceCodeBuilder sb) |
| 854 | + { |
| 855 | + var attr = domainPrimitiveType.GetAttributes() |
| 856 | + .FirstOrDefault(x => string.Equals(x.AttributeClass?.ToDisplayString(), Constants.PatternAttributeFullName, StringComparison.Ordinal)); |
| 857 | + |
| 858 | + if (attr is null) |
| 859 | + return; |
| 860 | + |
| 861 | + var pattern = (string)attr.ConstructorArguments[0].Value!; |
| 862 | + var validate = (bool)attr.ConstructorArguments[1].Value!; |
| 863 | + |
| 864 | + if (string.IsNullOrEmpty(pattern)) |
| 865 | + return; |
| 866 | + |
| 867 | + data.Pattern = pattern; |
| 868 | + data.ValidatePattern = validate; |
| 869 | + var quotedPattern = SymbolDisplay.FormatLiteral(data.Pattern, quote: true); |
| 870 | + |
| 871 | + if (validate) |
| 872 | + { |
| 873 | + sb.AppendLine($"if (!Regex.IsMatch(value, {quotedPattern}, RegexOptions.Compiled))") |
| 874 | + .AppendLine($"\tthrow InvalidDomainValueException.InvalidPatternException(typeof({data.ClassName}), value, {quotedPattern});") |
| 875 | + .NewLine(); |
| 876 | + } |
| 877 | + } |
839 | 878 | } |
0 commit comments