Skip to content
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

ST110 - StrictAssetsModelingRuleSchema #4519

Merged
merged 7 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/4519.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Added 'AssetsModelingRuleSchema' to the `ST110` validation.
type: feature
pr_number: 4519
38 changes: 34 additions & 4 deletions demisto_sdk/commands/content_graph/parsers/modeling_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
from pathlib import Path
from typing import List, Optional, Set

import pydantic

from demisto_sdk.commands.common.constants import MarketplaceVersions
from demisto_sdk.commands.common.tools import get_value
from demisto_sdk.commands.common.tools import get_file, get_value
from demisto_sdk.commands.content_graph.common import ContentType
from demisto_sdk.commands.content_graph.parsers.yaml_content_item import (
YAMLContentItemParser,
)
from demisto_sdk.commands.content_graph.strict_objects.modeling_rule import (
StrictModelingRule,
from demisto_sdk.commands.content_graph.strict_objects.assets_modeling_rule import (
StrictAssetsModelingRule,
)
from demisto_sdk.commands.content_graph.strict_objects.assets_modeling_rule_schema import (
StrictAssetsModelingRuleSchema,
)
from demisto_sdk.commands.content_graph.strict_objects.base_strict_model import (
StructureError,
)


Expand Down Expand Up @@ -43,4 +51,26 @@ def supported_marketplaces(self) -> Set[MarketplaceVersions]:

@property
def strict_object(self):
return StrictModelingRule
raise NotImplementedError("This object has a different behavior")

def validate_structure(self) -> List[StructureError]:
"""
This method uses the parsed data and attempts to build a Pydantic (strict) object from it.
Whenever the data and schema mismatch, we store the error using the 'structure_errors' attribute,
which will be read during the ST110 validation run.
In ModelingRule, we need to check two files: the schema json and the yml, so we override the
method for combing all the pydantic errors from the both files.
"""
directory_pydantic_error = []
directory = self.path if self.path.is_dir() else self.path.parent
for file in directory.iterdir():
try:
if file.suffix == ".yml":
StrictAssetsModelingRule.parse_obj(get_file(file))
elif file.suffix == ".json":
StrictAssetsModelingRuleSchema.parse_obj(get_file(file))
except pydantic.error_wrappers.ValidationError as e:
directory_pydantic_error += [
StructureError(path=file, **error) for error in e.errors()
]
return directory_pydantic_error
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Dict, Literal, Optional

from pydantic import Field, constr

from demisto_sdk.commands.content_graph.strict_objects.common import BaseStrictModel


class AssetType(BaseStrictModel):
type: Literal["string", "int", "float", "datetime", "boolean"] = Field(
description="Type of the asset"
)
is_array: bool = Field(description="Whether the asset is an array")


class StrictAssetsModelingRuleSchema(BaseStrictModel):
__root__: Optional[Dict[constr(regex=r".+"), Dict[str, AssetType]]] = None # type:ignore[valid-type]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r".+" means a string that isn't empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, since the field can be everything
as it allows in the schema file