Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/System.Management.Automation/engine/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,22 +1311,15 @@ protected override void Validate(object arguments, EngineIntrinsics engineIntrin

if (MinLength == MaxLength && len != MaxLength)
{
throw new ValidationMetadataException("ValidateCountNotExactlyEqual",
null, Metadata.ValidateCountFailure,
MinLength, MaxLength, len);
}

if (len < MinLength)
{
throw new ValidationMetadataException("ValidateCountSmallerThanMin",
null, Metadata.ValidateCountFailure,
MinLength, MaxLength, len);
throw new ValidationMetadataException("ValidateCountExactFailure",
null, Metadata.ValidateCountExactFailure,
MaxLength, len);
}

if (len > MaxLength)
if (len < MinLength || len > MaxLength)
{
throw new ValidationMetadataException("ValidateCountGreaterThanMax",
null, Metadata.ValidateCountFailure,
throw new ValidationMetadataException("ValidateCountMinMaxFailure",
null, Metadata.ValidateCountMinMaxFailure,
MinLength, MaxLength, len);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/System.Management.Automation/resources/Metadata.resx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@
<data name="ValidateCountNotInArray" xml:space="preserve">
<value>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.</value>
</data>
<data name="ValidateCountFailure" xml:space="preserve">
<data name="ValidateCountExactFailure" xml:space="preserve">
<value>The parameter requires exactly {0} value(s) - {1} value(s) were provided.</value>
</data>
<data name="ValidateCountMinMaxFailure" xml:space="preserve">
<value>The parameter requires at least {0} value(s) and no more than {1} value(s) - {2} value(s) were provided.</value>
</data>
<data name="ValidateCountMaxLengthSmallerThanMinLength" xml:space="preserve">
Expand Down
6 changes: 3 additions & 3 deletions test/powershell/engine/ValidateAttributes.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Describe 'Validate Attributes Tests' -Tags 'CI' {
@{ 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" }
@{ sb = { function Local:foo { param([ValidateCount(2, 2)] [string[]] $bar) }; foo 1 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountExactFailure" }
@{ sb = { function Local:foo { param([ValidateCount(2, 3)] [string[]] $bar) }; foo 1 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountMinMaxFailure" }
@{ sb = { function Local:foo { param([ValidateCount(2, 3)] [string[]] $bar) }; foo 1,2,3,4 }; FullyQualifiedErrorId = "ParameterArgumentValidationError,foo"; InnerErrorId = "ValidateCountMinMaxFailure" }
)
}

Expand Down