@@ -74,27 +74,25 @@ internal void AddValidationError(string key, string[] errors)
7474 ValidationErrors ??= [ ] ;
7575
7676 var formattedKey = FormatKey ( key ) ;
77- var formattedErrors = errors . Select ( FormatErrorMessage ) . ToArray ( ) ;
78- ValidationErrors [ formattedKey ] = formattedErrors ;
77+ ValidationErrors [ formattedKey ] = errors ;
7978 }
8079
8180 internal void AddOrExtendValidationErrors ( string key , string [ ] errors )
8281 {
8382 ValidationErrors ??= [ ] ;
8483
8584 var formattedKey = FormatKey ( key ) ;
86- var formattedErrors = errors . Select ( FormatErrorMessage ) . ToArray ( ) ;
8785
8886 if ( ValidationErrors . TryGetValue ( formattedKey , out var existingErrors ) )
8987 {
90- var newErrors = new string [ existingErrors . Length + formattedErrors . Length ] ;
88+ var newErrors = new string [ existingErrors . Length + errors . Length ] ;
9189 existingErrors . CopyTo ( newErrors , 0 ) ;
92- formattedErrors . CopyTo ( newErrors , existingErrors . Length ) ;
90+ errors . CopyTo ( newErrors , existingErrors . Length ) ;
9391 ValidationErrors [ formattedKey ] = newErrors ;
9492 }
9593 else
9694 {
97- ValidationErrors [ formattedKey ] = formattedErrors ;
95+ ValidationErrors [ formattedKey ] = errors ;
9896 }
9997 }
10098
@@ -103,15 +101,14 @@ internal void AddOrExtendValidationError(string key, string error)
103101 ValidationErrors ??= [ ] ;
104102
105103 var formattedKey = FormatKey ( key ) ;
106- var formattedError = FormatErrorMessage ( error ) ;
107104
108- if ( ValidationErrors . TryGetValue ( formattedKey , out var existingErrors ) && ! existingErrors . Contains ( formattedError ) )
105+ if ( ValidationErrors . TryGetValue ( formattedKey , out var existingErrors ) && ! existingErrors . Contains ( error ) )
109106 {
110- ValidationErrors [ formattedKey ] = [ .. existingErrors , formattedError ] ;
107+ ValidationErrors [ formattedKey ] = [ .. existingErrors , error ] ;
111108 }
112109 else
113110 {
114- ValidationErrors [ formattedKey ] = [ formattedError ] ;
111+ ValidationErrors [ formattedKey ] = [ error ] ;
115112 }
116113 }
117114
@@ -207,73 +204,5 @@ private string FormatComplexKey(string key)
207204 return result . ToString ( ) ;
208205 }
209206
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+
279208}
0 commit comments