Skip to content

Instantly share code, notes, and snippets.

@csandfeld
Created January 9, 2019 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csandfeld/d6c547b74fb41c701910370fd57d94c5 to your computer and use it in GitHub Desktop.
Save csandfeld/d6c547b74fb41c701910370fd57d94c5 to your computer and use it in GitHub Desktop.
Testing Custom Validators and Transforms
VERBOSE: Loading module from path 'C:\Custom Validators and Transforms\TestMod.psm1'.
VERBOSE: Exporting function 'Test-OwnClass'.
VERBOSE: Exporting function 'Test-ValidatePathExists'.
VERBOSE: Exporting function 'Test-PathTransform'.
VERBOSE: Importing function 'Test-OwnClass'.
VERBOSE: Importing function 'Test-PathTransform'.
VERBOSE: Importing function 'Test-ValidatePathExists'.
SomeString
----------
Some test string
Cannot find the type for custom attribute 'ValidatePathExists'. Make sure that the assembly that contains this type is loaded.
At C:\Custom Validators and Transforms\TestMod.psm1:69 char:9
+ [ValidatePathExists()]
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ([ValidatePathExists()]:AttributeAst) [], RuntimeException
+ FullyQualifiedErrorId : CustomAttributeTypeNotFound
Cannot find the type for custom attribute 'PathTransform'. Make sure that the assembly that contains this type is loaded.
At C:\Custom Validators and Transforms\TestMod.psm1:80 char:9
+ [PathTransform()]
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ([PathTransform()]:AttributeAst) [], RuntimeException
+ FullyQualifiedErrorId : CustomAttributeTypeNotFound
$module = [System.IO.Path]::Combine($PSScriptRoot, 'TestMod.psm1')
Import-Module -Name $module -Force -Verbose
Test-OwnClass -String 'Some test string'
Test-ValidatePathExists -Path $module
Test-PathTransform -Path $module
class OwnClass
{
[string] $SomeString
OwnClass ([string] $String)
{
$this.SomeString = $String
}
}
class ValidatePathExistsAttribute : System.Management.Automation.ValidateArgumentsAttribute
{
[void] Validate([object]$arguments, [System.Management.Automation.EngineIntrinsics]$engineIntrinsics)
{
$path = $arguments
if([string]::IsNullOrWhiteSpace($path))
{
Throw [System.ArgumentNullException]::new()
}
if(-not (Test-Path -Path $path))
{
Throw [System.IO.FileNotFoundException]::new()
}
}
}
class PathTransformAttribute : System.Management.Automation.ArgumentTransformationAttribute
{
[object] Transform([System.Management.Automation.EngineIntrinsics]$engineIntrinsics, [object] $inputData)
{
if ( $inputData -is [string] )
{
if ( -NOT [string]::IsNullOrWhiteSpace( $inputData ) )
{
$fullPath = Resolve-Path -Path $inputData -ErrorAction SilentlyContinue
if ( ( $fullPath.count -gt 0 ) -and ( -Not [string]::IsNullOrWhiteSpace( $fullPath ) ) )
{
return $fullPath.Path
}
}
}
$fullName = $inputData.Fullname
if($fullName.count -gt 0)
{
return $fullName
}
throw [System.IO.FileNotFoundException]::new()
}
}
function Test-OwnClass {
Param(
[OwnClass] $String
)
$String
}
function Test-ValidatePathExists {
Param(
[Parameter(Mandatory)]
[ValidatePathExists()]
$Path
)
$Path
}
function Test-PathTransform {
Param(
[Parameter(Mandatory)]
[PathTransform()]
$Path
)
$Path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment