diff --git a/test/unit/test_is_valid_string.py b/test/unit/test_is_valid_string.py new file mode 100644 index 000000000..bb4d2afba --- /dev/null +++ b/test/unit/test_is_valid_string.py @@ -0,0 +1,10 @@ +from tracardi.process_engine.action.v1.interest.utils import is_valid_string + + +def test_alphanumeric(): + assert is_valid_string("abc123") == True + assert is_valid_string("abc_123") == True + assert is_valid_string("abc-123") == True + assert is_valid_string("abc@123") == False + assert is_valid_string("___---") == False + assert is_valid_string("_") == False \ No newline at end of file diff --git a/tracardi/process_engine/action/v1/interest/decrease/plugin.py b/tracardi/process_engine/action/v1/interest/decrease/plugin.py index 96c4869ca..7a0d9cd9f 100644 --- a/tracardi/process_engine/action/v1/interest/decrease/plugin.py +++ b/tracardi/process_engine/action/v1/interest/decrease/plugin.py @@ -4,6 +4,7 @@ from tracardi.service.plugin.runner import ActionRunner from tracardi.service.plugin.domain.result import Result from ..config import Configuration +from ..utils import is_valid_string def validate(config: dict): @@ -28,7 +29,16 @@ async def run(self, payload: dict, in_edge=None) -> Result: return Result(value={"message": msg}, port="error") - self.profile.decrease_interest(self.config.interest, float(self.config.value)) + dot = self._get_dot_accessor(payload) + interest_key = dot[self.config.interest] + + if not is_valid_string(interest_key): + message = (f"Invalid interest name. Expected alpha-numeric string without spaces, got `{interest_key}`. " + f"Interest name must be an alpha-numeric string without spaces. Hyphen and dashes are allowed.") + self.console.error(message) + return Result(value={"message": message}, port="error") + + self.profile.decrease_interest(interest_key, float(self.config.value)) self.profile.operation.update = True return Result(port="payload", value=payload) @@ -54,7 +64,7 @@ def register() -> Plugin: id="interest", name="Interest name", description="Please type interest name.", - component=FormComponent(type="text", props={"label": "Interest name"}) + component=FormComponent(type="dotPath", props={"label": "Interest name"}) ), FormField( id="value", @@ -65,9 +75,10 @@ def register() -> Plugin: ] )] ), - version='0.8.0', + version='0.8.2', license="MIT + CC", - author="Risto Kowaczewski" + author="Risto Kowaczewski", + manual="decrease_interest" ), metadata=MetaData( name='Decrease interest', diff --git a/tracardi/process_engine/action/v1/interest/increase/plugin.py b/tracardi/process_engine/action/v1/interest/increase/plugin.py index 231538c5e..98dd16b66 100644 --- a/tracardi/process_engine/action/v1/interest/increase/plugin.py +++ b/tracardi/process_engine/action/v1/interest/increase/plugin.py @@ -4,6 +4,7 @@ from tracardi.service.plugin.runner import ActionRunner from tracardi.service.plugin.domain.result import Result from ..config import Configuration +from ..utils import is_valid_string def validate(config: dict): @@ -28,7 +29,16 @@ async def run(self, payload: dict, in_edge=None) -> Result: return Result(value={"message": msg}, port="error") - self.profile.increase_interest(self.config.interest, float(self.config.value)) + dot = self._get_dot_accessor(payload) + interest_key = dot[self.config.interest] + + if not is_valid_string(interest_key): + message = (f"Invalid interest name. Expected alpha-numeric string without spaces, got `{interest_key}`. " + f"Interest name must be an alpha-numeric string without spaces. Hyphen and dashes are allowed.") + self.console.error(message) + return Result(value={"message": message}, port="error") + + self.profile.increase_interest(interest_key, float(self.config.value)) self.profile.operation.update = True return Result(port="payload", value=payload) @@ -54,7 +64,7 @@ def register() -> Plugin: id="interest", name="Interest name", description="Please type interest name.", - component=FormComponent(type="text", props={"label": "Interest name"}) + component=FormComponent(type="dotPath", props={"label": "Interest name"}) ), FormField( id="value", @@ -65,9 +75,10 @@ def register() -> Plugin: ] )] ), - version='0.8.0', + version='0.8.2', license="MIT + CC", - author="Risto Kowaczewski" + author="Risto Kowaczewski", + manual="increase_interest" ), metadata=MetaData( name='Increase interest', diff --git a/tracardi/process_engine/action/v1/interest/utils.py b/tracardi/process_engine/action/v1/interest/utils.py new file mode 100644 index 000000000..22cba17b6 --- /dev/null +++ b/tracardi/process_engine/action/v1/interest/utils.py @@ -0,0 +1,21 @@ +def is_valid_string(variable): + """ + Check if the variable is a string composed of alphanumeric characters, underscores (_), and hyphens (-), + contains no spaces, and has at least one alphanumeric character. + + Args: + variable (str): The variable to be checked. + + Returns: + bool: True if the variable meets the criteria, False otherwise. + """ + if not isinstance(variable, str): + return False + + # Check for at least one alphanumeric character + if not any(char.isalnum() for char in variable): + return False + + # Checking if all characters are alphanumeric, underscore, or hyphen, and if there are no spaces + return all(char.isalnum() or char in ['_', '-'] for char in variable) and ' ' not in variable +