Title: Parameters to methods in classes should be able to take [VALIDATExxx] attributes. · Issue #2485 · PowerShell/PowerShell · GitHub
Open Graph Title: Parameters to methods in classes should be able to take [VALIDATExxx] attributes. · Issue #2485 · PowerShell/PowerShell
X Title: Parameters to methods in classes should be able to take [VALIDATExxx] attributes. · Issue #2485 · PowerShell/PowerShell
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"))...
Open Graph Description: Should be able to add validate attributes to params like: class NetHelper { static [String]ConvertToSubnetMask( [ValidateRange(0, 32)] [UInt32]$PrefixLength ) { $byteMask = ([Convert]::ToUInt32($((...
X Description: Should be able to add validate attributes to params like: class NetHelper { static [String]ConvertToSubnetMask( [ValidateRange(0, 32)] [UInt32]$PrefixLength ) { $byteMask = ([Convert]::ToUInt32($((...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/2485
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Parameters to methods in classes should be able to take [VALIDATExxx] attributes.","articleBody":"Should be able to add validate attributes to params like:\r\n\r\n```powershell\r\nclass NetHelper\r\n{\r\n static [String]ConvertToSubnetMask(\r\n [ValidateRange(0, 32)]\r\n [UInt32]$PrefixLength\r\n )\r\n {\r\n $byteMask = ([Convert]::ToUInt32($((\"1\" \\* $PrefixLength).PadRight(32, \"0\")), 2))\r\n $bytes = [BitConverter]::GetBytes($byteMask)\r\n [Array]::Reverse($bytes)\r\n $ipAddress = New-Object System.Net.IPAddress -ArgumentList (, $bytes)\r\n return $ipAddress.IPAddressToString\r\n }\r\n}\r\n```\r\n\r\nJust to be clear - I understand that we are creating a proper .net class and using real .Net call conventions. \r\nWhat I'm suggesting is that underneath the covers, we implement something along the lines of:\r\n\r\n```csharp\r\nclass NetHelper\r\n{\r\n static [String]ConvertToSubnetMask(\r\n [UInt32]$PrefixLength\r\n )\r\n {\r\n [ValidateRange(0, 32)]$LocalPrefixLength = $PrefixLength\r\n $byteMask = ([Convert]::ToUInt32($((\"1\" \\* $LocalPrefixLength).PadRight(32, \"0\")), 2))\r\n $bytes = [BitConverter]::GetBytes($byteMask)\r\n [Array]::Reverse($bytes)\r\n $ipAddress = New-Object System.Net.IPAddress -ArgumentList (, $bytes)\r\n return $ipAddress.IPAddressToString\r\n }\r\n}\r\n```\r\n\r\nError message example:\r\n```\r\nThe attribute cannot be added because variable LocalPrefixLength with value 35 would no longer be valid.\r\nAt line:8 char:8\r\n- [ValidateRange(0, 32)]$LocalPrefixLength = $PrefixLength\r\n- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n - CategoryInfo : MetadataError: (:) [], ValidationMetadataException\r\n - FullyQualifiedErrorId : ValidateSetFailure (edited)\r\n```\r\n\r\n\u003c!-- provide the output of $PSVersionTable -->\r\n\r\n``` powershell\r\n> $PSVersionTable\r\n\r\n\r\nName Value\r\n---- -----\r\nPSVersion 5.1.14393.206\r\nPSEdition Desktop\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}\r\nBuildVersion 10.0.14393.206\r\nCLRVersion 4.0.30319.42000\r\nWSManStackVersion 3.0\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\n```\r\n","author":{"url":"https://github.com/jpsnover","@type":"Person","name":"jpsnover"},"datePublished":"2016-10-14T19:15:36Z","url":"https://github.com/PowerShell/PowerShell/issues/2485"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ab4e38ab-a3c6-a8f3-e949-e78288ea0a2d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D9BE:21892F:E3BE3:14A135:6AFDC0C8 |
| html-safe-nonce | d06083f991771374535476a87afcb14ed6ea9d77d620074f18ac4381eb51305c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOUJFOjIxODkyRjpFM0JFMzoxNEExMzU6NkFGREMwQzgiLCJ2aXNpdG9yX2lkIjoiODIxMzUyNjY0MDY4MDg3ODI4MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1580cc59cc519c11da48142a31a7b3e887dc07b5770fb3a17faac5612f1794e5 |
| hovercard-subject-tag | issue:183129490 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/2485/issue_layout |
| twitter:image | https://opengraph.githubassets.com/978297b8bdd36f8a0316198aaf321359cf4be3a47c56d43472717938010db56e/PowerShell/PowerShell/issues/2485 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/978297b8bdd36f8a0316198aaf321359cf4be3a47c56d43472717938010db56e/PowerShell/PowerShell/issues/2485 |
| og:image:alt | Should be able to add validate attributes to params like: class NetHelper { static [String]ConvertToSubnetMask( [ValidateRange(0, 32)] [UInt32]$PrefixLength ) { $byteMask = ([Convert]::ToUInt32($((... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jpsnover |
| hostname | github.com |
| expected-hostname | github.com |
| None | 35c6bf57da1370dc64866160540c0ee9f825f291935b3bf6ec5492b00e35f3a6 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ab48c73a2978548773f5f24717ed9e58508befdb |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width