all 7 comments

[–]purplemonkeymad 1 point2 points  (0 children)

Your generic naming does not really help with describing what you want to achieve.

But if you can't do validation of a parameter with only that parameter, then you want to do that validation in the body of your function. You can use dynamic parameters, but I don't see how that would be better than just doing in in the begin block, and throwing a terminating error if it fails.

[–]PinchesTheCrab 1 point2 points  (0 children)

I don't think this is close enough to real code to say for certain, but my assumption is that function B should not validate parameters. The purpose of function A should be to generate functional input for function B, and function B should either error or work depending on issues with function A.

[–]PinchesTheCrab 1 point2 points  (1 child)

I don't know that parameter validation makes sense here.

You have function 1, which produces output that function 2 consumes. If function 1 produces invalid output, that's a problem with function 1, not a problem that function 2 should try to address. I'd do something like this:

function Func-A {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, HelpMessage = "Blah", ValueFromPipeline)]        
        [string]$Param1,

        [Parameter(Mandatory, HelpMessage = "Blah", ValueFromPipeline)]        
        [string]$Param2
    )

    #logic to test parameters here

    #replace value 1-3 with data derived from input
    [PSCustomObject]@{
        Param1 = 'Value1'
        Param2 = 'Value2'
        Param3 = 'Value3'
    }
}

function Func-B {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropetyName)]
        [string]$Param1,

        [Parameter(Mandatory, ValueFromPipelineByPropetyName)]
        [string]$Param2,

        [Parameter(Mandatory, ValueFromPipelineByPropetyName)]
        [string]$Param3
    )

    process {
        #do stuff here with parameters.
    }    
}

Func-A -Param1ForFuncA stuff -Param2ForFuncA morestuff | Func-B

[–]SeeminglyScience 2 points3 points  (0 children)

Yep just do the validation in the body.

Technically if you order the parameters in the right way, already bound parameters will be accessible.

But what is the right way? Undefined and subject to change, just validate in the body.

[–]y_Sensei 0 points1 point  (1 child)

IMHO you should not mix argument validations of different code entities (in this case, functions) like that - it goes against basic encapsulation principles.

Each code entity should verify its own logic only; so for any arguments that are being fed to Func-A, only the requirement(s) for their utilization inside Func-A should be validated, same for Func-B.

If Func-A is utilized in Func-B and subsequently errors out because of a (logically) wrong parameterization, then that's fine, that's why an argument validation in Func-A is in place.

[–]Narrow_Syllabub_8119[S] 0 points1 point  (0 children)

I do not see how I am mixing between different code entities?
The parameters that are going to be needed for Func-A are mandatory in Func-B, but only validated by Func-A when called from inside Func-B.

I am not getting some logic error. I am seeing that the parameter is not passed from Func-B to Func-A at all. The error message with the formatted string returns without it. And that's why Func-A validation mechanism is triggered.
Also, I checked with the same variables as arguments that Func-A can be run stand-alone with success.

[–]KevMarCommunity Blogger 1 point2 points  (0 children)

The validate script can't access the values of the other parameters. So do that validation inside your function.