Improve ValidateLength error message consistency and refactor validation tests by jorgeasaurus · Pull Request #25806 · PowerShell/PowerShell
(#25566) ## Problem The ValidateLengthMaxLengthFailure error message was displaying incorrectly, showing the length value where the parameter name should appear: - Actual: "The character length of the 5 argument is too long..." - Expected: "The character length of the "argument" argument is too long..." This occurred because the resource string was updated to expect three parameters (MaxLength, actual length, and parameter name) but the code was only passing two parameters, causing the format string to use the length value in place of the parameter name. ## Root Cause The resource string format was changed to include a parameter name placeholder: - Old format: "The character length of the {1} argument..." - New format: "The character length of the "{2}" argument is too long at {1} characters..." But the corresponding code change to pass the parameter name was not implemented. ## Solution 1. Updated ValidateLengthAttribute.ValidateElement() to pass "argument" as the third parameter when throwing ValidationMetadataException for max length validation failures. 2. Updated the ValidateLengthMaxLengthFailure resource string in Metadata.resx to properly format the error message with all three placeholders. While the current architecture doesn't support passing actual parameter names to validation attributes (which would require significant architectural changes), using "argument" as a generic placeholder prevents the confusing formatting error and provides a clear, consistent error message. ## Changes - Modified src/System.Management.Automation/engine/Attributes.cs to pass "argument" as third parameter in ValidateLength max length validation - Updated src/System.Management.Automation/resources/ Metadata.resx to include proper formatting for parameter name placeholder - Added comprehensive tests in test/powershell/engine /Basic/ValidateAttributes.Tests.ps1 to verify the fix ## Testing Added tests to verify: - Maximum length validation error formatting shows "argument" placeholder - Minimum length validation remains unchanged (different format) - Non-string parameter validation works correctly - Valid string lengths pass validation - Error message contains actual parameter name in outer exception The fix ensures error messages are properly formatted and readable while maintaining backward compatibility. Fixes #25566
iSazonov
added
the
CL-General
label
jorgeasaurus
changed the title
Fix ValidateLength error message formatting issue
Improve ValidateLength error message consistency and refactor validation tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters