◐ Shell
clean mode source ↗

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 iSazonov added the CL-General

Indicates that a PR should be marked as a general cmdlet change in the Change Log

label

Oct 6, 2025

iSazonov

iSazonov

@jorgeasaurus

iSazonov

@jorgeasaurus

iSazonov

@jorgeasaurus jorgeasaurus changed the title Fix ValidateLength error message formatting issue Improve ValidateLength error message consistency and refactor validation tests

Oct 12, 2025

iSazonov

SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request

Dec 12, 2025

kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request

Jan 2, 2026

JustinGrote pushed a commit to JustinGrote/PowerShell that referenced this pull request

Jun 2, 2026