Changing property value through validation?

Post Reply
User avatar
Soul Forge
Member
Posts: 61
Joined: Wed Jul 12, 2023 5:25 pm

Changing property value through validation?

Post by Soul Forge »

I notice that some apps validate it's properties when designing and when I use "Multi-line text" they usually join the array I input with ";".
How can I do that in my script?

I was looking at the flowElement methods, and there is no setPropertyStringValue or something like that.

Here's my current code:

Code: Select all

async function validateProperties(s: Switch, flowElement: FlowElement, tags: string[]): Promise<{ tag: string, valid: boolean }[]> {
    let resultArray: { tag: string, valid: boolean }[] = [];
    let tag: string;

    for (let i: number = 0; i < tags.length; i++) {
        tag = tags[i];
        const value = await flowElement.getPropertyStringValue(tag);

        if (/stringList\d+/.test(tag)) {
            await flowElement.getPropertyStringValue().
            await flowElement.log(LogLevel.Debug, typeof value == "object" ? value.join(";") : value);
            resultArray.push({ tag: tag, valid: true });
        }
    }

    return resultArray;
}
Using a logic that does that makes it easier for the rest of the code as I'll always get a string with ";" to separate the values.
jan_suhr
Advanced member
Posts: 687
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Changing property value through validation?

Post by jan_suhr »

It's better to assign the flowElement.getPropertyStringValue to a variable in the script and then use that variable for what ever you choose.

So in your If statement you have the variables instead.

There is also a function for validateProperties

https://www.enfocus.com/manuals/Develop ... oints.html
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
freddyp
Advanced member
Posts: 1129
Joined: Thu Feb 09, 2012 3:53 pm

Re: Changing property value through validation?

Post by freddyp »

Look at the possible return values of flowElement.getPropertyStringValue. It is a so-called overloaded function, which means that it can return more than one type of variable. In this case the return values can be string or string[]. For a single-line text you will get a string and for a multiline text you will get an array of strings. You do not have to do anything for that. If the property could return either one and your code has to do different things depending on whether it is a string or an array of strings, you can use Array.isArray(variable) to test that.
User avatar
tdeschampsBluewest
Member
Posts: 127
Joined: Tue Jun 01, 2021 11:57 am

Re: Changing property value through validation?

Post by tdeschampsBluewest »

Hi,
Here's our snippet for validating properties, with an additional function for handling a multi-line text table.

Note that "await flowElement.getPropertyStringValue(tag)" will return many different properties, and sometimes in a different form which can be misleading when using TS.

It is better to manipulate them on a specific function for transformers in the desired type (boolean, object with value tag pair, date...) rather than using the basic functions.

For example, if you allow "multiline text, number, and single line text", you can have a string or string[] when using getPropertyStringValue.

Code: Select all

declare namespace switchTypes {
    type propertyValidation = { tag: string; valid: boolean };
}

async function getArray(flowElement: FlowElement, tag: string, filterEmptyLine: boolean = true): Promise<any[]> {
    const value = await flowElement.getPropertyStringValue(tag);
    let array = value instanceof Array ? value : [value];
    if (filterEmptyLine) {
        array = array.filter((i) => i.trim() !== "");
    }
    return array;
}

async function validateProperties(s: Switch, flowElement: FlowElement, tags: string[]): Promise<switchTypes.propertyValidation[]> {
    let validateProperties: switchTypes.propertyValidation[] = [];
    let isValid: boolean;
    for (const tag of tags) {
        switch (tag) {
            case "myMultilineTag":
                let myArray = await getArray(flowElement, tag, true);
                isValid = //some code to change the isValid boolean depending on myArray
                break;
                
             //Add new case as needed for each validation
            default:
                isValid = true;
                break;
        }
        validateProperties.push({
            tag: tag,
            valid: isValid
        });
    }
    return validateProperties;
}

Do you like the Enfocus Apps developed by Bluewest?
Feel free to leave a comment on the Appstore!
Post Reply