|
3 | 3 |
|
4 | 4 | using System.ComponentModel.DataAnnotations;
|
5 | 5 | using System.Diagnostics.CodeAnalysis;
|
| 6 | +using System.Text.Json; |
6 | 7 |
|
7 | 8 | namespace Microsoft.AspNetCore.Http.Validation;
|
8 | 9 |
|
@@ -60,41 +61,139 @@ public sealed class ValidateContext
|
60 | 61 | /// </summary>
|
61 | 62 | public int CurrentDepth { get; set; }
|
62 | 63 |
|
| 64 | + /// <summary> |
| 65 | + /// Gets or sets the JSON serializer options to use for property name formatting. |
| 66 | + /// When set, property names in validation errors will be formatted according to the |
| 67 | + /// PropertyNamingPolicy and JsonPropertyName attributes. |
| 68 | + /// </summary> |
| 69 | + public JsonSerializerOptions? SerializerOptions { get; set; } |
| 70 | + |
63 | 71 | internal void AddValidationError(string key, string[] error)
|
64 | 72 | {
|
65 | 73 | ValidationErrors ??= [];
|
66 | 74 |
|
67 |
| - ValidationErrors[key] = error; |
| 75 | + var formattedKey = FormatKey(key); |
| 76 | + ValidationErrors[formattedKey] = error; |
68 | 77 | }
|
69 | 78 |
|
70 | 79 | internal void AddOrExtendValidationErrors(string key, string[] errors)
|
71 | 80 | {
|
72 | 81 | ValidationErrors ??= [];
|
73 | 82 |
|
74 |
| - if (ValidationErrors.TryGetValue(key, out var existingErrors)) |
| 83 | + var formattedKey = FormatKey(key); |
| 84 | + if (ValidationErrors.TryGetValue(formattedKey, out var existingErrors)) |
75 | 85 | {
|
76 | 86 | var newErrors = new string[existingErrors.Length + errors.Length];
|
77 | 87 | existingErrors.CopyTo(newErrors, 0);
|
78 | 88 | errors.CopyTo(newErrors, existingErrors.Length);
|
79 |
| - ValidationErrors[key] = newErrors; |
| 89 | + ValidationErrors[formattedKey] = newErrors; |
80 | 90 | }
|
81 | 91 | else
|
82 | 92 | {
|
83 |
| - ValidationErrors[key] = errors; |
| 93 | + ValidationErrors[formattedKey] = errors; |
84 | 94 | }
|
85 | 95 | }
|
86 | 96 |
|
87 | 97 | internal void AddOrExtendValidationError(string key, string error)
|
88 | 98 | {
|
89 | 99 | ValidationErrors ??= [];
|
90 | 100 |
|
91 |
| - if (ValidationErrors.TryGetValue(key, out var existingErrors) && !existingErrors.Contains(error)) |
| 101 | + var formattedKey = FormatKey(key); |
| 102 | + if (ValidationErrors.TryGetValue(formattedKey, out var existingErrors) && !existingErrors.Contains(error)) |
92 | 103 | {
|
93 |
| - ValidationErrors[key] = [.. existingErrors, error]; |
| 104 | + ValidationErrors[formattedKey] = [.. existingErrors, error]; |
94 | 105 | }
|
95 | 106 | else
|
96 | 107 | {
|
97 |
| - ValidationErrors[key] = [error]; |
| 108 | + ValidationErrors[formattedKey] = [error]; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private string FormatKey(string key) |
| 113 | + { |
| 114 | + if (string.IsNullOrEmpty(key) || SerializerOptions?.PropertyNamingPolicy is null) |
| 115 | + { |
| 116 | + return key; |
| 117 | + } |
| 118 | + |
| 119 | + // If the key contains a path (e.g., "Address.Street" or "Items[0].Name"), |
| 120 | + // apply the naming policy to each part of the path |
| 121 | + if (key.Contains('.') || key.Contains('[')) |
| 122 | + { |
| 123 | + return FormatComplexKey(key); |
98 | 124 | }
|
| 125 | + |
| 126 | + // For JsonPropertyName attribute support, we'd need property info |
| 127 | + // but for basic usage, apply the naming policy directly |
| 128 | + return SerializerOptions.PropertyNamingPolicy.ConvertName(key); |
| 129 | + } |
| 130 | + |
| 131 | + private string FormatComplexKey(string key) |
| 132 | + { |
| 133 | + // Use a more direct approach for complex keys with dots and array indices |
| 134 | + var result = new System.Text.StringBuilder(); |
| 135 | + int lastIndex = 0; |
| 136 | + int i = 0; |
| 137 | + bool inBracket = false; |
| 138 | + |
| 139 | + while (i < key.Length) |
| 140 | + { |
| 141 | + char c = key[i]; |
| 142 | + |
| 143 | + if (c == '[') |
| 144 | + { |
| 145 | + // Format the segment before the bracket |
| 146 | + if (i > lastIndex) |
| 147 | + { |
| 148 | + string segment = key.Substring(lastIndex, i - lastIndex); |
| 149 | + string formattedSegment = SerializerOptions!.PropertyNamingPolicy!.ConvertName(segment); |
| 150 | + result.Append(formattedSegment); |
| 151 | + } |
| 152 | + |
| 153 | + // Start collecting the bracket part |
| 154 | + inBracket = true; |
| 155 | + result.Append(c); |
| 156 | + lastIndex = i + 1; |
| 157 | + } |
| 158 | + else if (c == ']') |
| 159 | + { |
| 160 | + // Add the content inside the bracket as-is |
| 161 | + if (i > lastIndex) |
| 162 | + { |
| 163 | + string segment = key.Substring(lastIndex, i - lastIndex); |
| 164 | + result.Append(segment); |
| 165 | + } |
| 166 | + result.Append(c); |
| 167 | + inBracket = false; |
| 168 | + lastIndex = i + 1; |
| 169 | + } |
| 170 | + else if (c == '.' && !inBracket) |
| 171 | + { |
| 172 | + // Format the segment before the dot |
| 173 | + if (i > lastIndex) |
| 174 | + { |
| 175 | + string segment = key.Substring(lastIndex, i - lastIndex); |
| 176 | + string formattedSegment = SerializerOptions!.PropertyNamingPolicy!.ConvertName(segment); |
| 177 | + result.Append(formattedSegment); |
| 178 | + } |
| 179 | + result.Append(c); |
| 180 | + lastIndex = i + 1; |
| 181 | + } |
| 182 | + |
| 183 | + i++; |
| 184 | + } |
| 185 | + |
| 186 | + // Format the last segment if there is one |
| 187 | + if (lastIndex < key.Length) |
| 188 | + { |
| 189 | + string segment = key.Substring(lastIndex); |
| 190 | + if (!inBracket) |
| 191 | + { |
| 192 | + segment = SerializerOptions!.PropertyNamingPolicy!.ConvertName(segment); |
| 193 | + } |
| 194 | + result.Append(segment); |
| 195 | + } |
| 196 | + |
| 197 | + return result.ToString(); |
99 | 198 | }
|
100 | 199 | }
|
0 commit comments