Skip to content

Implement optional custom error messages for *all* validation parameter attributes (not just ValidateScript and ValidatePattern) #3745

@mklement0

Description

@mklement0

This is a follow-up to #3630.
Related: #3765

Thanks to @powercode's efforts in #2728, , we now have an optional ErrorMessage property in the ValidatePattern and ValidateScript parameter validation attributes. However, all validation attributes (Validation*; all that derive from System.Automation.Management.ValidateArgumentsAttribute) should support a custom error message:

  • Even for the simplest validations it can sometimes be helpful to provide domain-specific guidance rather than reporting a purely technical violation (see example below).

  • Consistent support eliminates the burden of having to remember which specific attributes support the property (though IntelliSense may ease that pain).

Example

function foo {
  param(
  [ValidateCount(2, [int]::maxvalue, ErrorMessage='This bar has a 2-drink minimum.')]
  [string] $bar
  )
  $bar
}

foo -bar 'mint julep'

Desired behavior

foo : Cannot validate argument on parameter 'bar'. This bar has a 2-drink minimum.
...

As in the implementation for ValidatePattern and ValidateScript, placeholder {0} would represent the value passed by the user, and {1}, ... the validation-attribute arguments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions