-
Notifications
You must be signed in to change notification settings - Fork 87
is_valid_types validation #5095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
inbalapt1
wants to merge
13
commits into
master
Choose a base branch
from
validation_types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fc7acc7
is_valid_types validation
inbalapt1 ac1e9e7
pre-commit + sdk_validation_config.toml
inbalapt1 6157009
changelog
inbalapt1 c06e1a7
fix
inbalapt1 1081e93
test
inbalapt1 39a1bd9
unittest
inbalapt1 3fb288c
Merge branch 'master' into validation_types
inbalapt1 5f8e039
fix pre-commit
inbalapt1 fae6b6b
Merge branch 'validation_types' of https://github.com/demisto/demisto…
inbalapt1 152141b
Merge branch 'master' into validation_types
inbalapt1 c01390f
remove deleted validation from tests
inbalapt1 a4abcac
Merge branch 'validation_types' of https://github.com/demisto/demisto…
inbalapt1 aed81d2
pre-commit
inbalapt1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| changes: | ||
| - description: Added AG105 to validate that the types of arguments and outputs are valid. | ||
| type: internal | ||
| pr_number: 5095 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,6 +1,10 @@ | ||||||
| from pathlib import Path | ||||||
|
|
||||||
| from demisto_sdk.commands.content_graph.objects.agentix_action import AgentixAction | ||||||
| from demisto_sdk.commands.content_graph.objects.agentix_action import ( | ||||||
| AgentixAction, | ||||||
| AgentixActionArgument, | ||||||
| AgentixActionOutput, | ||||||
| ) | ||||||
| from demisto_sdk.commands.content_graph.objects.agentix_agent import AgentixAgent | ||||||
| from demisto_sdk.commands.content_graph.objects.script import Script | ||||||
| from demisto_sdk.commands.validate.validators.AG_validators.AG100_is_forbidden_content_item import ( | ||||||
|
|
@@ -9,6 +13,9 @@ | |||||
| from demisto_sdk.commands.validate.validators.AG_validators.AG101_is_correct_mp import ( | ||||||
| IsCorrectMPValidator, | ||||||
| ) | ||||||
| from demisto_sdk.commands.validate.validators.AG_validators.AG105_is_valid_types import ( | ||||||
| IsTypeValid, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def test_is_forbidden_content_item(): | ||||||
|
|
@@ -215,3 +222,172 @@ def test_is_correct_marketplace(): | |||||
| assert results[0].message == ( | ||||||
| "The following Agentix related content item 'test' should have only marketplace 'platform'." | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def test_is_type_valid(): | ||||||
| """ | ||||||
| Given | ||||||
| - One AgentixAction with valid argument and output types. | ||||||
| - One AgentixAction with invalid argument and output types. | ||||||
| - One AgentixAction with mixed valid and invalid types. | ||||||
|
|
||||||
| When | ||||||
| - Calling the IsTypeValid obtain_invalid_content_items function. | ||||||
|
|
||||||
| Then | ||||||
| - Ensure that 2 validation failures are returned. | ||||||
| - Make sure the error messages include the invalid argument and output names, | ||||||
| and list the valid type options. | ||||||
| """ | ||||||
| # Valid content item | ||||||
| valid_action = AgentixAction( | ||||||
| color="red", | ||||||
| description="", | ||||||
| display="", | ||||||
| path=Path("test_valid.yml"), | ||||||
| marketplaces=["platform"], | ||||||
| name="valid_action", | ||||||
| fromversion="", | ||||||
| toversion="", | ||||||
| display_name="ValidAction", | ||||||
| deprecated=False, | ||||||
| id="", | ||||||
| node_id="", | ||||||
| underlying_content_item_id="test", | ||||||
| underlying_content_item_name="test", | ||||||
| underlying_content_item_type="script", | ||||||
| underlying_content_item_version=-1, | ||||||
| agent_id="test", | ||||||
| args=[ | ||||||
| AgentixActionArgument( | ||||||
| name="arg1", description="arg1", type="string", underlyingargname="arg1" | ||||||
| ), | ||||||
| AgentixActionArgument( | ||||||
| name="arg2", | ||||||
| description="arg2", | ||||||
| type="boolean", | ||||||
| underlyingargname="arg2", | ||||||
| ), | ||||||
| ], | ||||||
| outputs=[ | ||||||
| AgentixActionOutput( | ||||||
| name="output1", | ||||||
| type="json", | ||||||
| description="output1", | ||||||
| underlyingoutputcontextpath="output1", | ||||||
| ), | ||||||
| AgentixActionOutput( | ||||||
| name="output2", | ||||||
| type="number", | ||||||
| description="output2", | ||||||
| underlyingoutputcontextpath="output2", | ||||||
| ), | ||||||
| ], | ||||||
| ) | ||||||
|
|
||||||
| # Invalid types for both args and outputs | ||||||
| invalid_action = AgentixAction( | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same in here. |
||||||
| color="red", | ||||||
| description="", | ||||||
| display="", | ||||||
| path=Path("test_invalid.yml"), | ||||||
| marketplaces=["platform"], | ||||||
| name="invalid_action", | ||||||
| fromversion="", | ||||||
| toversion="", | ||||||
| display_name="InvalidAction", | ||||||
| deprecated=False, | ||||||
| id="", | ||||||
| node_id="", | ||||||
| underlying_content_item_id="test", | ||||||
| underlying_content_item_name="test", | ||||||
| underlying_content_item_type="script", | ||||||
| underlying_content_item_version=-1, | ||||||
| agent_id="test", | ||||||
| args=[ | ||||||
| AgentixActionArgument( | ||||||
| name="arg_invalid", | ||||||
| type="InvalidType", | ||||||
| description="arg_invalid", | ||||||
| underlyingargname="arg_invalid", | ||||||
| ), | ||||||
| ], | ||||||
| outputs=[ | ||||||
| AgentixActionOutput( | ||||||
| name="output_invalid", | ||||||
| type="Object", | ||||||
| description="output_invalid", | ||||||
| underlyingoutputcontextpath="output_invalid", | ||||||
| ), | ||||||
| ], | ||||||
| ) | ||||||
|
|
||||||
| # Mixed valid and invalid | ||||||
| mixed_action = AgentixAction( | ||||||
| color="red", | ||||||
| description="", | ||||||
| display="", | ||||||
| path=Path("test_mixed.yml"), | ||||||
| marketplaces=["platform"], | ||||||
| name="mixed_action", | ||||||
| fromversion="", | ||||||
| toversion="", | ||||||
| display_name="MixedAction", | ||||||
| deprecated=False, | ||||||
| id="", | ||||||
| node_id="", | ||||||
| underlying_content_item_id="test", | ||||||
| underlying_content_item_name="test", | ||||||
| underlying_content_item_type="script", | ||||||
| underlying_content_item_version=-1, | ||||||
| agent_id="test", | ||||||
| args=[ | ||||||
| AgentixActionArgument( | ||||||
| name="arg_ok", | ||||||
| type="number", | ||||||
| description="arg_ok", | ||||||
| underlyingargname="arg_ok", | ||||||
| ), | ||||||
| AgentixActionArgument( | ||||||
| name="arg_bad", | ||||||
| type="Blob", | ||||||
| description="arg_bad", | ||||||
| underlyingargname="arg_bad", | ||||||
| ), | ||||||
| ], | ||||||
| outputs=[ | ||||||
| AgentixActionOutput( | ||||||
| name="output_ok", | ||||||
| type="string", | ||||||
| description="output_ok", | ||||||
| underlyingoutputcontextpath="output_ok", | ||||||
| ), | ||||||
| AgentixActionOutput( | ||||||
| name="output_bad", | ||||||
| type="Array", | ||||||
| description="output_bad", | ||||||
| underlyingoutputcontextpath="output_bad", | ||||||
| ), | ||||||
| ], | ||||||
| ) | ||||||
|
|
||||||
| content_items = [valid_action, invalid_action, mixed_action] | ||||||
|
|
||||||
| results = IsTypeValid().obtain_invalid_content_items(content_items) | ||||||
|
|
||||||
| # We expect 2 invalid results: invalid_action and mixed_action | ||||||
| assert len(results) == 2 | ||||||
|
|
||||||
| # Validate first message content | ||||||
| assert ( | ||||||
| "The following Agentix action 'InvalidAction' contains invalid types:\n" | ||||||
| "Arguments with invalid types: arg_invalid. Possible argument types: unknown, keyValue, textArea, string, number, date, boolean.\nOutputs with invalid types: output_invalid. " | ||||||
| "Possible output types: unknown, string, number, date, boolean, json." | ||||||
| ) in results[0].message | ||||||
|
|
||||||
| # Validate second message content | ||||||
| assert ( | ||||||
| "The following Agentix action 'MixedAction' contains invalid types:\n" | ||||||
| "Arguments with invalid types: arg_bad. Possible argument types: unknown, keyValue, textArea, string, number, date, boolean.\n" | ||||||
| "Outputs with invalid types: output_bad. Possible output types: unknown, string, number, date, boolean, json." | ||||||
| ) in results[1].message | ||||||
103 changes: 103 additions & 0 deletions
103
demisto_sdk/commands/validate/validators/AG_validators/AG105_is_valid_types.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| from typing import Iterable, List, Optional, Set | ||
|
|
||
| from demisto_sdk.commands.content_graph.objects import ( | ||
| AgentixAction, | ||
| ) | ||
| from demisto_sdk.commands.content_graph.objects.agentix_action import ( | ||
| AgentixActionArgument, | ||
| AgentixActionOutput, | ||
| ) | ||
| from demisto_sdk.commands.validate.validators.base_validator import ( | ||
| BaseValidator, | ||
| ValidationResult, | ||
| ) | ||
|
|
||
| ContentTypes = AgentixAction | ||
|
|
||
| args_valid_types = [ | ||
| "unknown", | ||
| "keyValue", | ||
| "textArea", | ||
| "string", | ||
| "number", | ||
| "date", | ||
| "boolean", | ||
| ] | ||
|
|
||
| outputs_valid_types = ["unknown", "string", "number", "date", "boolean", "json"] | ||
|
|
||
|
|
||
| class IsTypeValid(BaseValidator[ContentTypes]): | ||
| error_code = "AG105" | ||
| description = "Ensures that all arguments and outputs use valid data types from the closed list." | ||
| rationale = "Helps the LLM understand and process data correctly according to its expected type." | ||
| error_message = "The following Agentix action '{0}' contains invalid types:\n{1}" | ||
|
|
||
| def obtain_invalid_content_items( | ||
| self, content_items: Iterable[ContentTypes] | ||
| ) -> List[ValidationResult]: | ||
| validation_results: List[ValidationResult] = [] | ||
| for content_item in content_items: | ||
| final_message = "" | ||
|
|
||
| # Check invalid arguments types | ||
| if invalid_args_types := self.is_invalid_args_type( | ||
| content_item.args, args_valid_types | ||
| ): | ||
| final_message += ( | ||
| f"Arguments with invalid types: {', '.join(invalid_args_types)}. " | ||
| f"Possible argument types: {', '.join(args_valid_types)}.\n" | ||
| ) | ||
|
|
||
| # Check invalid outputs types | ||
| if invalid_outputs_types := self.is_invalid_outputs_type( | ||
| content_item.outputs, outputs_valid_types | ||
| ): | ||
| final_message += ( | ||
| f"Outputs with invalid types: {', '.join(invalid_outputs_types)}. " | ||
| f"Possible output types: {', '.join(outputs_valid_types)}." | ||
| ) | ||
|
|
||
| if final_message: | ||
| validation_results.append( | ||
| ValidationResult( | ||
| validator=self, | ||
| message=self.error_message.format( | ||
| content_item.display_name, | ||
| final_message, | ||
| ), | ||
| content_object=content_item, | ||
| ) | ||
| ) | ||
|
|
||
| return validation_results | ||
|
|
||
| def is_invalid_args_type( | ||
| self, | ||
| elements: Optional[List[AgentixActionArgument]], | ||
| valid_types: List[str], | ||
| ) -> Set[str]: | ||
| invalid_element_names: Set[str] = set() | ||
| if elements is None: | ||
| return invalid_element_names | ||
|
|
||
| for element in elements: | ||
| if element.type.lower() not in valid_types: | ||
| invalid_element_names.add(element.name) | ||
|
|
||
| return invalid_element_names | ||
|
|
||
| def is_invalid_outputs_type( | ||
| self, | ||
| elements: Optional[List[AgentixActionOutput]], | ||
| valid_types: List[str], | ||
| ) -> Set[str]: | ||
| invalid_element_names: Set[str] = set() | ||
| if elements is None: | ||
| return invalid_element_names | ||
|
|
||
| for element in elements: | ||
| if element.type.lower() not in valid_types: | ||
| invalid_element_names.add(element.name) | ||
|
|
||
| return invalid_element_names |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try to use create_agentix_action_object() insted.