Most validation parameter attribute types cause the AllowNull attribute to be ignored
See original GitHub issueSteps to reproduce
& { param([AllowNull()] $Foo) $null -eq $Foo } $null
& { param([AllowNull()] [ValidateScript({$true})] $Foo) $null -eq $Foo } $null
& { param([AllowNull()] [ValidateLength(0, 10)] $Foo) $null -eq $Foo } $null
& { param([AllowNull()] [ValidateRange(1,2)] $Foo) $null -eq $Foo } $null
& { param([AllowNull()] [ValidatePattern('.*')] $Foo) $null -eq $Foo } $null
& { param([AllowNull()] [ValidateSet(1,2)] $Foo) $null -eq $Foo } $null
& { param([AllowNull()] [ValidateDrive("c:")] $Foo) $null -eq $Foo } $null
Expected behavior
True
True
True
True
True
True
True
To clarify, I expected the following behavior, which I think is a useful combination of AllowNull and other validation attributes:
If [AllowNull()] is (also) present:
- If
$nullis actually being passed, allow the value and ignore any other validation attributes. - Otherwise, evaluate any other validation attributes as usual.
Actual behavior
True
param [...] : Cannot validate argument on parameter 'Foo'. The argument is null, empty, or an element of the argument. [...]
[...]
param[...] : Cannot validate argument on parameter 'Foo'. The argument is null, empty, or an element of the argument. [...]
[...]
param[...] : Cannot validate argument on parameter 'Foo'. The argument is null, empty, or an element of the argument. [...]
[...]
param[...] : Cannot validate argument on parameter 'Foo'. The argument is null, empty, or an element of the argument. [...]
[...]
param[...] : Cannot validate argument on parameter 'Foo'. The argument is null, empty, or an element of the argument. [...]
[...]
param[...] : Cannot validate argument on parameter 'Foo'. The argument is null.
The AllowNull attribute is effectively ignored in the presence of the validation attributes in the snippet; switching the attribute order makes no difference.
Note how the last error message, triggered by the ValidateDrive attribute, differs.
The following validation attributes can be combined with AllowNull:
ValidateCountAllowEmptyStringAllowEmptyCollection
Environment data
PowerShell Core v6.0.0-beta.3 on macOS 10.12.5
PowerShell Core v6.0.0-beta.3 on Ubuntu 16.04.1 LTS
PowerShell Core v6.0.0-beta.3 on Microsoft Windows 10 Pro (64-bit; v10.0.14393)
Windows PowerShell v5.1.15063.413 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Fody/NullGuard: Adds null argument checks to an assembly
Conditional postcondition attributes (ie. [MaybeNullWhenAttribute] ) that indicate the value may sometimes be null causes the postcondition null check to be ...
Read more >c# - What does it mean for a property to be [Required] and ...
The reason for making a property nullable and marked with the [Required] attribute is to protect against under-posting attacks.
Read more >about Functions Advanced Parameters - PowerShell
AllowNull validation attribute The following example declares a hashtable ComputerInfo parameter that can have a null value. The AllowNull ...
Read more >C# 11.0 preview: parameter null checking
In this post I'm going to describe what this new language feature does, and how it relates to and is distinct from nullable...
Read more >Model validation in ASP.NET Core MVC and Razor Pages
Validation attributes let you specify validation rules for model ... Use a nullable reference type to allow null or missing values to be ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yes, but only if
$nullis being passed; in other words:If
[AllowNull()]is (also) present:$nullis actually being passed, allow the value and ignore any other validation attributes.@iSazonov:
Passing
$nulltoparam([AllowNull()] [ValidateScript({$true})] $Foo)satisfies both attributes, for instance, yet it fails as described.By contrast, passing
$nulltoparam([AllowNull()] [ValidateCount(0,1)] $Foo)works.To me it makes sense to allow these combinations, assuming the
AllowNullattribute is explicitly specified.