@@ -74,27 +74,25 @@ internal void AddValidationError(string key, string[] errors)
74
74
ValidationErrors ??= [ ] ;
75
75
76
76
var formattedKey = FormatKey ( key ) ;
77
- var formattedErrors = errors . Select ( FormatErrorMessage ) . ToArray ( ) ;
78
- ValidationErrors [ formattedKey ] = formattedErrors ;
77
+ ValidationErrors [ formattedKey ] = errors ;
79
78
}
80
79
81
80
internal void AddOrExtendValidationErrors ( string key , string [ ] errors )
82
81
{
83
82
ValidationErrors ??= [ ] ;
84
83
85
84
var formattedKey = FormatKey ( key ) ;
86
- var formattedErrors = errors . Select ( FormatErrorMessage ) . ToArray ( ) ;
87
85
88
86
if ( ValidationErrors . TryGetValue ( formattedKey , out var existingErrors ) )
89
87
{
90
- var newErrors = new string [ existingErrors . Length + formattedErrors . Length ] ;
88
+ var newErrors = new string [ existingErrors . Length + errors . Length ] ;
91
89
existingErrors . CopyTo ( newErrors , 0 ) ;
92
- formattedErrors . CopyTo ( newErrors , existingErrors . Length ) ;
90
+ errors . CopyTo ( newErrors , existingErrors . Length ) ;
93
91
ValidationErrors [ formattedKey ] = newErrors ;
94
92
}
95
93
else
96
94
{
97
- ValidationErrors [ formattedKey ] = formattedErrors ;
95
+ ValidationErrors [ formattedKey ] = errors ;
98
96
}
99
97
}
100
98
@@ -103,15 +101,14 @@ internal void AddOrExtendValidationError(string key, string error)
103
101
ValidationErrors ??= [ ] ;
104
102
105
103
var formattedKey = FormatKey ( key ) ;
106
- var formattedError = FormatErrorMessage ( error ) ;
107
104
108
- if ( ValidationErrors . TryGetValue ( formattedKey , out var existingErrors ) && ! existingErrors . Contains ( formattedError ) )
105
+ if ( ValidationErrors . TryGetValue ( formattedKey , out var existingErrors ) && ! existingErrors . Contains ( error ) )
109
106
{
110
- ValidationErrors [ formattedKey ] = [ .. existingErrors , formattedError ] ;
107
+ ValidationErrors [ formattedKey ] = [ .. existingErrors , error ] ;
111
108
}
112
109
else
113
110
{
114
- ValidationErrors [ formattedKey ] = [ formattedError ] ;
111
+ ValidationErrors [ formattedKey ] = [ error ] ;
115
112
}
116
113
}
117
114
@@ -207,73 +204,5 @@ private string FormatComplexKey(string key)
207
204
return result . ToString ( ) ;
208
205
}
209
206
210
- // Format validation error messages to use the same property naming policy as the keys
211
- private string FormatErrorMessage ( string errorMessage )
212
- {
213
- if ( SerializerOptions ? . PropertyNamingPolicy is null )
214
- {
215
- return errorMessage ;
216
- }
217
-
218
- // Pattern 1: "The {PropertyName} field is required."
219
- const string pattern1Start = "The " ;
220
- const string pattern1Middle = " field " ;
221
-
222
- // Pattern 2: "The field {PropertyName} must be between X and Y."
223
- const string pattern2 = "The field " ;
224
-
225
- // Try Pattern 1 first
226
- if ( errorMessage . StartsWith ( pattern1Start , StringComparison . Ordinal ) )
227
- {
228
- int endIndex = errorMessage . IndexOf ( pattern1Middle , pattern1Start . Length , StringComparison . Ordinal ) ;
229
- if ( endIndex > pattern1Start . Length )
230
- {
231
- // Extract the property name between "The " and " field "
232
- ReadOnlySpan < char > messageSpan = errorMessage . AsSpan ( ) ;
233
- ReadOnlySpan < char > propertyNameSpan = messageSpan . Slice ( pattern1Start . Length , endIndex - pattern1Start . Length ) ;
234
- string propertyName = propertyNameSpan . ToString ( ) ;
235
-
236
- if ( ! string . IsNullOrWhiteSpace ( propertyName ) )
237
- {
238
- // Format the property name with the naming policy
239
- string formattedPropertyName = SerializerOptions . PropertyNamingPolicy . ConvertName ( propertyName ) ;
240
-
241
- // Construct the new error message by combining parts
242
- return string . Concat (
243
- pattern1Start ,
244
- formattedPropertyName ,
245
- messageSpan . Slice ( endIndex ) . ToString ( )
246
- ) ;
247
- }
248
- }
249
- }
250
- // Try Pattern 2
251
- else if ( errorMessage . StartsWith ( pattern2 , StringComparison . Ordinal ) )
252
- {
253
- // Find the word after "The field " and before " must"
254
- const string pattern2End = " must" ;
255
- int startPos = pattern2 . Length ;
256
- int endPos = errorMessage . IndexOf ( pattern2End , startPos , StringComparison . Ordinal ) ;
257
-
258
- if ( endPos > startPos )
259
- {
260
- string propertyName = errorMessage . Substring ( startPos , endPos - startPos ) ;
261
- if ( ! string . IsNullOrWhiteSpace ( propertyName ) )
262
- {
263
- // Format the property name with the naming policy
264
- string formattedPropertyName = SerializerOptions . PropertyNamingPolicy . ConvertName ( propertyName ) ;
265
-
266
- // Reconstruct the message
267
- ReadOnlySpan < char > errorSpan = errorMessage . AsSpan ( ) ;
268
- return string . Concat (
269
- pattern2 ,
270
- formattedPropertyName ,
271
- errorSpan . Slice ( endPos ) ) ;
272
- }
273
- }
274
- }
275
-
276
- // Return the original message if no patterns matched or formatting failed
277
- return errorMessage ;
278
- }
207
+
279
208
}
0 commit comments