TM-ValidationUtility.psm1

using namespace System.IO
using namespace System.Management.Automation


enum PathType {
    File
    Folder
    Any
}


class PathNotFoundException : IOException {
    PathNotFoundException($Message) : base($Message) {}
}


class ValidatePathExistsAttribute : ValidateArgumentsAttribute {
    $_pathType = [PathType]::Any

    ValidatePathExistsAttribute() { }

    ValidatePathExistsAttribute([PathType]$PathType) {
        $this._pathType = $PathType
    }

    [void]Validate([object]$object, [EngineIntrinsics]$engineIntrinsics) {
        # check whether the path is empty:
        $path = if ($object.GetType() -is [FileSystemInfo]) {
            $object.FullName
        } else {
            $object.ToString()
        }

        if ([string]::IsNullOrWhiteSpace($path)) {
            throw [System.ArgumentNullException]::new()
        }

        if (
            ($this._pathType -eq [PathType]::Any) -and
            ([File]::Exists($path) -eq $false) -and
            ([Directory]::Exists($path) -eq $false)
        ) {
            throw [PathNotFoundException]::new("Path '$path' does not exist")
        } elseif (($this._pathType -eq [PathType]::File) -and ([File]::Exists($path) -eq $false)) {
            throw [FileNotFoundException]::new("File '$path' does not exist")
        } elseif (($this._pathType -eq [PathType]::Directory) -and ([Directory]::Exists($path) -eq $false)) {
            throw [DirectoryNotFoundException]::new("Directory '$path' does not exist")
        }
    }
}


class InvalidFormatException : Exception {
    InvalidFormatException($Message) : base($Message) {}
}


class ValidateIPv4FormatAttribute : ValidateArgumentsAttribute {
    $Ipv4Regex = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

    [void]Validate([object]$object, [EngineIntrinsics]$engineIntrinsics) {
        # check whether the path is empty:
        if ([string]::IsNullOrWhiteSpace($object)) {
            throw [System.ArgumentNullException]::new('ValidateIPv4FormatAttribute can only validate strings.')
        } elseif ($object.GetType().FullName -ne 'System.String') {
            throw [System.InvalidOperationException]::new('')
        } elseif ($object -NotMatch $this.Ipv4Regex) {
            throw [InvalidFormatException]::new("Invalid format for '$Object'. Does not match Ipv4 format.")
        }
    }
}


class ValidateGistUriFormatAttribute : ValidateArgumentsAttribute {
    # Match https://gist.github.com/$username/$Guid or https://api.github.com/gists/$Guid
    $GistUriRegex = '^(https://gist\.github\.com/[[a-zA-Z0-9-_]+|https://api\.github\.com/gists)/[a-zA-Z0-9]{32}$'

    [void]Validate([object]$object, [EngineIntrinsics]$engineIntrinsics) {
        # check whether the path is empty:
        if ([string]::IsNullOrWhiteSpace($object)) {
            throw [System.ArgumentNullException]::new('ValidateIPv4FormatAttribute can only validate strings.')
        } elseif ($object.GetType().FullName -ne 'System.String') {
            throw [System.InvalidOperationException]::new('')
        } elseif ($object -NotMatch $this.GistUriRegex) {
            throw [InvalidFormatException]::new("Invalid format for '$Object'. Does not match Gist Uri format.")
        }
    }
}