-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,69 @@ | ||
import pytest | ||
|
||
from demisto_sdk.commands.validate.tests.test_tools import create_generic_field_object | ||
from demisto_sdk.commands.validate.validators.GF_validators.GF100_generic_field_group import ( | ||
REQUIRED_GROUP_VALUE, | ||
GenericFieldGroupValidator, | ||
) | ||
from demisto_sdk.commands.validate.validators.GF_validators.GF101_generic_field_id_prefix_validate import ( | ||
GenericFieldIdPrefixValidateValidator, | ||
) | ||
|
||
|
||
def test_GenericFieldIdPrefixValidateValidator_is_valid(): | ||
""" | ||
Given: | ||
- GenericField content items | ||
When: | ||
- run is_valid method | ||
Then: | ||
- Ensure that the ValidationResult returned | ||
for the GenericField whose 'id' without `generic_` prefix | ||
- Ensure that no ValidationResult returned | ||
when `id` field with `generic_` prefix | ||
""" | ||
generic_field = create_generic_field_object(paths=["id"], values=["foo"]) | ||
|
||
# not valid | ||
results = GenericFieldIdPrefixValidateValidator().is_valid([generic_field]) | ||
assert results[0].message == "foo is not a valid id, it should start with generic_." | ||
|
||
# valid | ||
generic_field.object_id = "generic_foo" | ||
assert not GenericFieldIdPrefixValidateValidator().is_valid([generic_field]) | ||
|
||
|
||
def test_GenericFieldGroupValidator_is_valid(): | ||
""" | ||
Given: | ||
- GenericField content items | ||
When: | ||
- run is_valid method | ||
Then: | ||
- Ensure that the ValidationResult returned | ||
for the GenericField whose 'group' field is not valid | ||
- Ensure that no ValidationResult returned when group field set to 4 | ||
""" | ||
# not valid | ||
generic_field = create_generic_field_object(paths=["group"], values=[0]) | ||
assert GenericFieldGroupValidator().is_valid([generic_field]) | ||
|
||
# valid | ||
generic_field.group = REQUIRED_GROUP_VALUE | ||
assert not GenericFieldGroupValidator().is_valid([generic_field]) | ||
|
||
|
||
def test_GenericFieldGroupValidator_fix(): | ||
""" | ||
Given: | ||
- invalid GenericField that 'group' field is not 4 | ||
When: | ||
- run fix method | ||
Then: | ||
- Ensure the fix message as expected | ||
- Ensure the field `group` is set to 4 | ||
""" | ||
generic_field = create_generic_field_object(paths=["group"], values=["0"]) | ||
result = GenericFieldGroupValidator().fix(generic_field) | ||
assert result.message == f"set the `group` field to {REQUIRED_GROUP_VALUE}." | ||
assert generic_field.group == REQUIRED_GROUP_VALUE |
This file contains 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
42 changes: 42 additions & 0 deletions
42
demisto_sdk/commands/validate/validators/GF_validators/GF100_generic_field_group.py
This file contains 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,42 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Iterable, List | ||
|
||
from demisto_sdk.commands.content_graph.objects.generic_field import GenericField | ||
from demisto_sdk.commands.validate.validators.base_validator import ( | ||
BaseValidator, | ||
FixResult, | ||
ValidationResult, | ||
) | ||
|
||
REQUIRED_GROUP_VALUE = 4 | ||
ContentTypes = GenericField | ||
|
||
|
||
class GenericFieldGroupValidator(BaseValidator[ContentTypes]): | ||
error_code = "GF100" | ||
rationale = "Required by the platform." | ||
description = f"Checks if group field is set to {REQUIRED_GROUP_VALUE}." | ||
error_message = f"The `group` key must be set to {REQUIRED_GROUP_VALUE}" | ||
fix_message = f"set the `group` field to {REQUIRED_GROUP_VALUE}." | ||
related_field = "group" | ||
is_auto_fixable = True | ||
|
||
def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]: | ||
return [ | ||
ValidationResult( | ||
validator=self, | ||
message=self.error_message, | ||
content_object=content_item, | ||
) | ||
for content_item in content_items | ||
if (content_item.group != REQUIRED_GROUP_VALUE) | ||
] | ||
|
||
def fix(self, content_item: ContentTypes) -> FixResult: | ||
content_item.group = REQUIRED_GROUP_VALUE | ||
return FixResult( | ||
validator=self, | ||
message=self.fix_message, | ||
content_object=content_item, | ||
) |
37 changes: 37 additions & 0 deletions
37
..._sdk/commands/validate/validators/GF_validators/GF101_generic_field_id_prefix_validate.py
This file contains 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,37 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Iterable, List | ||
|
||
from demisto_sdk.commands.content_graph.objects.generic_field import GenericField | ||
from demisto_sdk.commands.validate.validators.base_validator import ( | ||
BaseValidator, | ||
FixResult, | ||
ValidationResult, | ||
) | ||
|
||
GENERIC_FIELD_ID_PREFIX = "generic_" | ||
ContentTypes = GenericField | ||
|
||
|
||
class GenericFieldIdPrefixValidateValidator(BaseValidator[ContentTypes]): | ||
error_code = "GF101" | ||
rationale = "Required by the platform." | ||
description = "Checks if the id starts with `generic_`." | ||
error_message = ( | ||
"{generic_id} is not a valid id, it should start with {generic_id_prefix}." | ||
) | ||
related_field = "id" | ||
|
||
def is_valid(self, content_items: Iterable[ContentTypes]) -> List[ValidationResult]: | ||
return [ | ||
ValidationResult( | ||
validator=self, | ||
message=self.error_message.format( | ||
generic_id=content_item.object_id, | ||
generic_id_prefix=GENERIC_FIELD_ID_PREFIX, | ||
), | ||
content_object=content_item, | ||
) | ||
for content_item in content_items | ||
if (not content_item.object_id.startswith(GENERIC_FIELD_ID_PREFIX)) | ||
] |