question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Most validation parameter attribute types cause the AllowNull attribute to be ignored

See original GitHub issue

Steps 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 $null is 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:

  • ValidateCount
  • AllowEmptyString
  • AllowEmptyCollection

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:open
  • Created 6 years ago
  • Reactions:7
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
mklement0commented, Jul 11, 2017

Yes, but only if $null is being passed; in other words:

If [AllowNull()] is (also) present:

  • If $null is actually being passed, allow the value and ignore any other validation attributes.
  • Otherwise, evaluate any other validation attributes as usual.
2reactions
mklement0commented, Jul 10, 2017

@iSazonov:

Passing $null to param([AllowNull()] [ValidateScript({$true})] $Foo) satisfies both attributes, for instance, yet it fails as described.

By contrast, passing $null to param([AllowNull()] [ValidateCount(0,1)] $Foo) works.

To me it makes sense to allow these combinations, assuming the AllowNull attribute is explicitly specified.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found