diff --git a/src/System.Management.Automation/engine/Attributes.cs b/src/System.Management.Automation/engine/Attributes.cs
index 189fc6032f..eb0f673ada 100644
--- a/src/System.Management.Automation/engine/Attributes.cs
+++ b/src/System.Management.Automation/engine/Attributes.cs
@@ -1309,18 +1309,25 @@ namespace System.Management.Automation
null, Metadata.ValidateCountNotInArray);
}
+ if (MinLength == MaxLength && len != MaxLength)
+ {
+ throw new ValidationMetadataException("ValidateCountNotExactlyEqual",
+ null, Metadata.ValidateCountFailure,
+ MinLength, MaxLength, len);
+ }
+
if (len < MinLength)
{
throw new ValidationMetadataException("ValidateCountSmallerThanMin",
- null, Metadata.ValidateCountMinLengthFailure,
- MinLength, len);
+ null, Metadata.ValidateCountFailure,
+ MinLength, MaxLength, len);
}
if (len > MaxLength)
{
throw new ValidationMetadataException("ValidateCountGreaterThanMax",
- null, Metadata.ValidateCountMaxLengthFailure,
- MaxLength, len);
+ null, Metadata.ValidateCountFailure,
+ MinLength, MaxLength, len);
}
}
diff --git a/src/System.Management.Automation/resources/Metadata.resx b/src/System.Management.Automation/resources/Metadata.resx
index 96ecd548e0..e6329a5443 100644
--- a/src/System.Management.Automation/resources/Metadata.resx
+++ b/src/System.Management.Automation/resources/Metadata.resx
@@ -144,11 +144,8 @@
The ValidateCount attribute cannot be applied to a non-array parameter. Either remove the attribute from the parameter or make the parameter an array parameter.
-
- The number of provided arguments ({1}) is fewer than the minimum number of allowed arguments ({0}). Provide more than {0} arguments, and then try the command again.
-
-
- The number of provided arguments, ({1}), exceeds the maximum number of allowed arguments ({0}). Provide fewer than {0} arguments, and then try the command again.
+
+ The parameter requires at least {0} value(s) and no more than {1} value(s) - {2} value(s) were provided.
The specified maximum number of arguments for a parameter is fewer than the specified minimum number of arguments. Update the ValidateCount attribute for the parameter.
@@ -228,4 +225,4 @@
The path argument has no root drive. Supply a full path argument with a root drive.
-
\ No newline at end of file
+
diff --git a/test/powershell/engine/ValidateAttributes.Tests.ps1 b/test/powershell/engine/ValidateAttributes.Tests.ps1
new file mode 100644
index 0000000000..a98ca17567
--- /dev/null
+++ b/test/powershell/engine/ValidateAttributes.Tests.ps1
@@ -0,0 +1,30 @@
+Import-Module $PSScriptRoot\..\Common\Test.Helpers.psm1
+
+Describe 'Validate Attributes Tests' -Tags 'CI' {
+
+ Context "ValidateCount" {
+ BeforeAll {
+ $testCases = @(
+ @{ sb = { function Local:foo { param([ValidateCount(-1,2)] [string[]] $bar) }; foo }; FullyQualifiedErrorId = "ExceptionConstructingAttribute"; InnerErrorId = "" }
+ @{ sb = { function Local:foo { param([ValidateCount(1,-1)] [string[]] $bar) }; foo }; FullyQualifiedErrorId = "ExceptionConstructingAttribute"; InnerErrorId = "" }
+ @{ sb = { function Local:foo { param([ValidateCount(2, 1)] [string[]] $bar) }; foo }; FullyQualifiedErrorId = "ValidateRangeMaxLengthSmallerThanMinLength"; InnerErrorId = "" }
+ @{ sb = { function Local:foo { param([ValidateCount(2, 2)] [string[]] $bar) }; foo 1 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountNotExactlyEqual" }
+ @{ sb = { function Local:foo { param([ValidateCount(2, 3)] [string[]] $bar) }; foo 1 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountSmallerThanMin" }
+ @{ sb = { function Local:foo { param([ValidateCount(2, 3)] [string[]] $bar) }; foo 1,2,3,4 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountGreaterThanMax" }
+ )
+ }
+
+ It 'Exception: :' -TestCases $testCases {
+ param($sb, $FullyQualifiedErrorId, $InnerErrorId)
+
+ $sb | ShouldBeErrorId $FullyQualifiedErrorId
+ if ($InnerErrorId) {
+ $error[0].exception.innerexception.errorrecord.FullyQualifiedErrorId | Should Be $InnerErrorId
+ }
+ }
+
+ It 'No Exception: valid argument count' {
+ { function Local:foo { param([ValidateCount(2, 4)] [string[]] $bar) }; foo 1,2,3,4 } | Should Not Throw
+ }
+ }
+}