Skip to content

Commit

Permalink
Merge e69167e into 419a05f
Browse files Browse the repository at this point in the history
  • Loading branch information
Shellyber committed Jun 20, 2024
2 parents 419a05f + e69167e commit b2e2404
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .changelog/4368.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Combined MR101 and MR102 into the new validation format. Validates that the modeling rule yml file contains the schema and rules keys and that they are empty.
type: internal
pr_number: 4368
5 changes: 5 additions & 0 deletions demisto_sdk/commands/content_graph/objects/modeling_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pathlib import Path
from typing import Optional

from pydantic.fields import Field

from demisto_sdk.commands.common.constants import (
MarketplaceVersions,
)
Expand All @@ -20,6 +22,9 @@


class ModelingRule(ContentItemXSIAM, content_type=ContentType.MODELING_RULE): # type: ignore[call-arg]
rules_key: Optional[str] = Field(default="", alias="rules")
schema_key: Optional[str] = Field(default="", alias="schema")

def summary(
self,
marketplace: Optional[MarketplaceVersions] = None,
Expand Down
13 changes: 12 additions & 1 deletion demisto_sdk/commands/content_graph/parsers/modeling_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List, Optional, Set

from demisto_sdk.commands.common.constants import MarketplaceVersions
from demisto_sdk.commands.common.tools import get_value
from demisto_sdk.commands.content_graph.common import ContentType
from demisto_sdk.commands.content_graph.parsers.yaml_content_item import (
YAMLContentItemParser,
Expand All @@ -20,9 +21,19 @@ def __init__(

@cached_property
def field_mapping(self):
super().field_mapping.update({"object_id": "id"})
super().field_mapping.update(
{"object_id": "id", "schema_key": "schema", "rules_key": "rules"}
)
return super().field_mapping

@property
def schema_key(self) -> Optional[str]:
return get_value(self.yml_data, self.field_mapping.get("schema_key", ""))

@property
def rules_key(self) -> Optional[str]:
return get_value(self.yml_data, self.field_mapping.get("rules_key", ""))

@property
def supported_marketplaces(self) -> Set[MarketplaceVersions]:
return {MarketplaceVersions.MarketplaceV2}
4 changes: 2 additions & 2 deletions demisto_sdk/commands/validate/sdk_validation_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ select = [
"RP101", "RP102",
"DA100", "DA101",
"IT100", "IT101", "IT102", "IT103",
"MR100",
"MR100", "MR101",
]
warning = []

Expand All @@ -66,7 +66,7 @@ select = [
"IM101", "IM108", "IM111",
"RP101", "RP102",
"IT102", "IT103",
"MR100",
"MR100", "MR101",
]
warning = [
"IN101",
Expand Down
38 changes: 38 additions & 0 deletions demisto_sdk/commands/validate/tests/MR_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from demisto_sdk.commands.validate.validators.MR_validators.MR100_validate_schema_file_exists import (
ValidateSchemaFileExistsValidator,
)
from demisto_sdk.commands.validate.validators.MR_validators.MR101_validate_empty_keys import (
ValidateEmptyKeysValidator,
)


def test_ValidateSchemaFileExistsValidator_is_valid():
Expand All @@ -27,3 +30,38 @@ def test_ValidateSchemaFileExistsValidator_is_valid():
'The modeling rule "Duo Modeling Rule" is missing a schema file.'
== results[0].message
)


def test_ValidateEmptyKeysValidator_is_valid():
"""
Given:
- Modeling Rules content items
When:
- run is_valid method
Then:
- Ensure that no ValidationResult returned when modeling rule has the right keys.
- Ensure that the ValidationResult returned when:
- One of the keys has a value in it (test instead of empty string).
- One of the keys does not exist as all.
"""
modeling_rule = create_modeling_rule_object()
# Valid
assert not ValidateEmptyKeysValidator().is_valid([modeling_rule])

# Case where there is a value in schema key
modeling_rule.schema_key = "test"
results = ValidateEmptyKeysValidator().is_valid([modeling_rule])
assert (
"Either the 'rules' key or the 'schema' key are missing or not empty, "
"make sure to set the values of these keys to an empty string."
== results[0].message
)

# Case where the rules key does not exist.
modeling_rule.rules_key = None
results = ValidateEmptyKeysValidator().is_valid([modeling_rule])
assert (
"Either the 'rules' key or the 'schema' key are missing or not empty, "
"make sure to set the values of these keys to an empty string."
== results[0].message
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import annotations

from typing import Iterable, List

from demisto_sdk.commands.content_graph.objects.modeling_rule import ModelingRule
from demisto_sdk.commands.validate.validators.base_validator import (
BaseValidator,
ValidationResult,
)

ContentTypes = ModelingRule


class ValidateEmptyKeysValidator(BaseValidator[ContentTypes]):
error_code = "MR101"
description = 'Validate that the modeling rules keys - "rules" and "schema" exist and are empty'
rationale = "This validation is for compatibility resaons. Without those fields the modeling rules won't work."
error_message = (
"Either the 'rules' key or the 'schema' key are missing or not empty, "
"make sure to set the values of these keys to an empty string."
)
related_field = "modeling rule"

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.schema_key != "" or content_item.rules_key != ""
]

0 comments on commit b2e2404

Please sign in to comment.