Skip to content

Parameters to methods in classes should be able to take [VALIDATExxx] attributes. #2485

Open

Description

Should be able to add validate attributes to params like:

class NetHelper
{
   static [String]ConvertToSubnetMask(
           [ValidateRange(0, 32)]
           [UInt32]$PrefixLength
   )
   {
       $byteMask = ([Convert]::ToUInt32($(("1" \* $PrefixLength).PadRight(32, "0")), 2))
       $bytes = [BitConverter]::GetBytes($byteMask)
       [Array]::Reverse($bytes)
       $ipAddress = New-Object System.Net.IPAddress -ArgumentList (, $bytes)
       return $ipAddress.IPAddressToString
   }
}

Just to be clear - I understand that we are creating a proper .net class and using real .Net call conventions.
What I'm suggesting is that underneath the covers, we implement something along the lines of:

class NetHelper
{
  static [String]ConvertToSubnetMask(
          [UInt32]$PrefixLength
  )
  {
      [ValidateRange(0, 32)]$LocalPrefixLength = $PrefixLength
      $byteMask = ([Convert]::ToUInt32($(("1" \* $LocalPrefixLength).PadRight(32, "0")), 2))
      $bytes = [BitConverter]::GetBytes($byteMask)
      [Array]::Reverse($bytes)
      $ipAddress = New-Object System.Net.IPAddress -ArgumentList (, $bytes)
      return $ipAddress.IPAddressToString
  }
}

Error message example:

The attribute cannot be added because variable LocalPrefixLength with value 35 would no longer be valid.
At line:8 char:8
-        [ValidateRange(0, 32)]$LocalPrefixLength = $PrefixLength
-        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  - CategoryInfo          : MetadataError: (:) [], ValidationMetadataException
  - FullyQualifiedErrorId : ValidateSetFailure (edited)
> $PSVersionTable


Name                           Value
----                           -----
PSVersion                      5.1.14393.206
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.206
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. added
    Up-for-Grabs
    on Oct 14, 2016
  2. added
    WG-Engine
    on Oct 14, 2016
  3. oising commented on Oct 26, 2016

    @oising
    Contributor

    If someone does this, please widen also the validator attributes' usage constraints at the class level to include parameters (they are now constrained to property,field only currently) rather than special-case them when parsing the AST/emitting the type.

  4. iSazonov commented on Feb 21, 2017

    @iSazonov
    Collaborator

    Oisin Grehan (@oising) Please give examples for clarity.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. added
    Resolution-No Activity
    on Nov 16, 2023
  8. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  10. added
    KeepOpen
    and removed
    Resolution-No Activity
    on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    KeepOpenUp-for-GrabsWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions