Workshop - Check if inputs are empty and form a string

What i want to do is to check if inputs have a value and the inputs that don´t have a value will form a string.

Example:
Input1: Value1
Input2: Value2
Input3: empty
Input4: empty

The string will be "Following inputs are empty: Input3, Input4.

This should be easy, but i find the GUI for variable handling in Workshop a little strange to use sometimes :laughing:

Hey there @bobbyb!

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;
    }

}

tested it and it works as intended:

Hope this helps!

Best,

1 Like

I was curious to see if it could be solved in Workshop, without using a function.
But maybe i just do it this way instead :slight_smile:

Thanks!

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.

Rgr that! I will write a function instead.
Thanks

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.