Workshop variable transformations can get cumbersome to set up when you need to do some complex multiple-variable checking. Luckily, you can achieve this quite easily with a typescript-backed function.
For the following example, I’m setting the Inputs 1-4 as different data types, but you should be able to adapt this easily:
import { Double, Function } from "@foundry/functions-api";
interface OptionalInputs{
Input1?: string,
Input2?: Double,
Input3?: boolean,
Input4?: Double
}
export class myFunctions{
@Function()
public checkInputs(Input1?: string, Input2?: Double, Input3?: boolean, Input4?: Double): string {
let returnString: string;
let InputCollection = {Input1, Input2, Input3, Input4}
const emptyInputs = Object.keys(InputCollection)
.filter(key => (InputCollection[key as keyof OptionalInputs] === undefined
|| InputCollection[key as keyof OptionalInputs] === ""
|| InputCollection[key as keyof OptionalInputs] === null));
returnString = emptyInputs.length > 0 ? `Following inputs are empty: ${emptyInputs.join(', ')}` : 'All inputs are provided';
return returnString;
}
}
You can do this in a Variable Transform type String variable, however it’d be a bit cumbersome.
For each input you’d need a boolean reference that checks if it’s null, then a conditional reference that either returns an empty string, in the case the corresponding boolean reference indicates the input is not null, or the display string for that inputs label.
Then you would use a final string concatenation reference to join together all of those conditional references with the rest of the standard text.
However, if I were doing this, I think I would write a small helper function in Typescript or Python like in the above answer.
To do this without Typescript, a string variable defined via Variable Transformation can be set up as shown below. Before using the screenshot, first define an empty, static string variable “(undefined)” to be used where empty string outputs are required.
One caveat is that this method can’t format strings with the .join(", “) method. More if/else blocks could check for when to add commas, but personally, if I cared about the formatting, I would use the variable transformation to output a string array variable instead, and then feed that into a small Typescript function that takes in the string array, applies join(”, "), and outputs the resulting string.