Skip to content

Support localization of custom error messages for parameter validation attributes #3765

@mklement0

Description

@mklement0

Now that at least some of the custom error messages for parameter validation attributes support custom error messages - see #3745, it should be possible to support localization of these error messages - currently, only literal strings are supported.

Without this ability, localized scripts are effectively prevented from using such validation attributes.

What currently works:

function foo {
  param(
    [ValidatePattern('o', ErrorMessage = 'Must have an "o".')]
    $bar
  )
  $bar
}

foo -bar baz

This yields:

foo : Cannot validate argument on parameter 'bar'. Must have an "o".

What support for localization might look like (not sure about feasibility; solely based on noticing that some attributes accept script blocks):

$stringTable = DATA {
  @{
    msg = 'Must have an "o".'
  }
}

function foo {
  param(
    [ValidatePattern('o', ErrorMessage = { $stringTable.msg })]
    $bar
  )
  $bar
}

foo -bar baz

Environment data

PowerShell Core v6.0.0-beta (v6.0.0-beta.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-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