Skip to content

Commit c0547b6

Browse files
Copilotalerickson
andcommitted
Add ValidateSet attribute to ApiVersion parameter and remove redundant validation code
Co-authored-by: alerickson <[email protected]>
1 parent 5c9d21e commit c0547b6

File tree

4 files changed

+4
-10
lines changed

4 files changed

+4
-10
lines changed

src/code/RegisterPSResourceRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class RegisterPSResourceRepository : PSCmdlet, IDynamicParameters
9191
/// Specifies the Api version of the repository to be set.
9292
/// </summary>
9393
[Parameter(ParameterSetName = NameParameterSet)]
94+
[ValidateSet("V2", "V3", "Local", "NugetServer", "ContainerRegistry")]
9495
public PSRepositoryInfo.APIVersion ApiVersion { get; set; }
9596

9697
/// <summary>

src/code/SetPSResourceRepository.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public SwitchParameter Trusted
8888
/// Specifies the Api version of the repository to be set.
8989
/// </summary>
9090
[Parameter(ParameterSetName = NameParameterSet)]
91+
[ValidateSet("V2", "V3", "Local", "NugetServer", "ContainerRegistry")]
9192
public PSRepositoryInfo.APIVersion ApiVersion { get; set; }
9293

9394
/// <summary>
@@ -162,14 +163,6 @@ protected override void ProcessRecord()
162163
PSRepositoryInfo.APIVersion? repoApiVersion = null;
163164
if (MyInvocation.BoundParameters.ContainsKey(nameof(ApiVersion)))
164165
{
165-
if (ApiVersion == PSRepositoryInfo.APIVersion.Unknown)
166-
{
167-
ThrowTerminatingError(new ErrorRecord(
168-
new ArgumentException("ApiVersion 'Unknown' is not a valid value for Set-PSResourceRepository. Valid values are: V2, V3, Local, NugetServer, ContainerRegistry"),
169-
"InvalidApiVersion",
170-
ErrorCategory.InvalidArgument,
171-
this));
172-
}
173166
repoApiVersion = ApiVersion;
174167
}
175168

test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ Describe "Test Register-PSResourceRepository" -tags 'CI' {
405405
}
406406

407407
It "should throw error when trying to register repository with ApiVersion unknown" {
408-
{Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "InvalidApiVersion,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository"
408+
{Register-PSResourceRepository -Name $TestRepoName1 -Uri $tmpDir1Path -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository"
409409

410410
# Verify the repository was not created
411411
$repo = Get-PSResourceRepository $TestRepoName1 -ErrorAction SilentlyContinue

test/ResourceRepositoryTests/SetPSResourceRepository.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ Describe "Test Set-PSResourceRepository" -tags 'CI' {
351351
$repoApiVersion = $repo.ApiVersion
352352
$repoApiVersion | Should -Be "local"
353353

354-
{Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "InvalidApiVersion,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository"
354+
{Set-PSResourceRepository -Name $TestRepoName1 -ApiVersion "unknown" -ErrorAction Stop} | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository"
355355

356356
# Verify the repository ApiVersion was not changed
357357
$repo = Get-PSResourceRepository $TestRepoName1

0 commit comments

Comments
 (0)