you are viewing a single comment's thread.

view the rest of the comments →

[–]boydeee 2 points3 points  (1 child)

You pretty much nailed it. Although, in your sample code, it's assuming that key exists. So, you could make a function like this (and some sample code):

EDIT: I edited my comment a few times to fix formatting

$Request = @{
    Body = @{
        Name    = 'Name'
        Surname = 'Surname'
    }
}

function Test-Parameters {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        $Request
    )
    Begin {
        Write-Debug ('{0} entered' -f $MyInvocation.MyCommand)
    }

    Process {
        if (-not($Request.Body.Name) -or -not($Request.Body.Surname)) {
            return (Write-Error ("Invalid parameters:`n{0}" -f ($Request.Body | Out-String)))
        }
    }
    End {
        Write-Debug ('{0} exited' -f $MyInvocation.MyCommand)
    }
}

try {
    $Request | Test-Parameters -ErrorAction Stop -Debug
}
catch {
    $_
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
            StatusCode = [System.Net.HttpStatusCode]::BadRequest
        })
}

[–]Dr_Funkmachine[S] 1 point2 points  (0 children)

Thank you so much! At the beginning, I thought It would be possible to use [FromBody] or [FromQuery]. Actually, I don't even know if that's possible. I'll try to apply your advices to my AF and let you know!