Skip to content
Open
Show file tree
Hide file tree
Changes from all 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/5079.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Add validators to enforce standardized id/name fields.
type: internal
pr_number: 5079
2 changes: 2 additions & 0 deletions TestSuite/layout_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def __init__(
def create_default_layout_rule(self):
self.write_json(
{
"id": self.rule_id,
"rule_id": self.rule_id,
"name": self.name,
"rule_name": self.name,
"layout_id": "test_layout",
"description": "",
Expand Down
3 changes: 3 additions & 0 deletions TestSuite/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ def __init__(self, name: str, trigger_dir_path: Path, json_content: dict = None)
super().__init__(trigger_dir_path, name, "")

if json_content:
# Write exactly what the test provided; do not auto-normalize.
self.write_json(json_content)
else:
self.create_default_trigger()

def create_default_trigger(self):
self.write_json(
{
"id": self.id,
"trigger_id": self.id,
"name": self.id,
"playbook_id": "mock playbook",
"suggestion_reason": "mock reason",
"description": "desc",
Expand Down
1 change: 1 addition & 0 deletions TestSuite/xdrc_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def write_dict(self, yml: dict):
def create_default(self):
self.write_json(
{
"id": self.id,
"content_global_id": self.id,
"name": self.id,
"os_type": "os_type_test",
Expand Down
1 change: 1 addition & 0 deletions TestSuite/xsiam_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def create_default(self):
{
"dashboards_data": [
{
"id": self.id,
"name": self.id,
"description": "mock dashboard desc",
"status": "ENABLED",
Expand Down
2 changes: 2 additions & 0 deletions TestSuite/xsiam_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def create_default(self):
{
"templates_data": [
{
"id": self.id,
"global_id": self.id,
"name": self.id,
"report_name": self.id,
"report_description": None,
"default_template_id": None,
Expand Down
10 changes: 9 additions & 1 deletion demisto_sdk/commands/common/schemas/layoutrule.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
type: map
mapping:
id:
type: str
required: true
rule_id:
type: str
required: false
# DEPRECATED: Use 'id' field instead. This field is kept for backward compatibility with existing content items.
name:
type: str
required: true
rule_name:
type: str
required: true
required: false
# DEPRECATED: Use 'name' field instead. This field is kept for backward compatibility with existing content items.
layout_id:
type: str
required: true
Expand Down
12 changes: 10 additions & 2 deletions demisto_sdk/commands/common/schemas/trigger.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
type: map
mapping:
id:
type: str
required: false
trigger_id:
type: str
required: true
required: false
# DEPRECATED: Use 'id' field instead. This field is kept for backward compatibility with existing content items.
fromVersion:
type: str
toVersion:
type: str
name:
type: str
required: false
trigger_name:
type: str
required: true
required: false
# DEPRECATED: Use 'name' field instead. This field is kept for backward compatibility with existing content items.
playbook_id:
type: str
required: true
Expand Down
6 changes: 5 additions & 1 deletion demisto_sdk/commands/common/schemas/xdrctemplate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ mapping:
name:
type: str
required: true
content_global_id:
id:
type: str
required: true
content_global_id:
type: str
required: false
# DEPRECATED: Use 'id' field instead. This field is kept for backward compatibility with existing content items.
from_xdr_version:
type: str
required: true
Expand Down
6 changes: 5 additions & 1 deletion demisto_sdk/commands/common/schemas/xsiamdashboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ mapping:
schema;dashboards_data_schema:
type: map
mapping:
global_id:
id:
type: str
required: true
global_id:
type: str
required: false
# DEPRECATED: Use 'id' field instead. This field is kept for backward compatibility with existing content items.
status:
type: str
required: true
Expand Down
10 changes: 9 additions & 1 deletion demisto_sdk/commands/common/schemas/xsiamreport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ schema;templates_data_schema:
mapping:
metadata:
type: str
id:
type: str
required: true
global_id:
type: str
required: true
report_name:
# DEPRECATED: Use 'id' field instead. This field is kept for backward compatibility with existing content items.
name:
type: str
required: true
report_name:
type: str
required: false
# DEPRECATED: Use 'name' field instead. This field is kept for backward compatibility with existing content items.
report_description:
type: str
required: false
Expand Down
12 changes: 6 additions & 6 deletions demisto_sdk/commands/common/tests/layout_rule_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def test_is_valid_file(repo):
dummy_layout_rule = pack.create_layout_rule(
"MyRule",
{
"rule_id": "test_rule",
"id": "test_rule",
"layout_id": "test_layout_id",
"description": "This trigger is test",
"rule_name": "test rule name",
"name": "test rule name",
"alerts_filter": {
"filter": {
"AND": [
Expand Down Expand Up @@ -50,10 +50,10 @@ def test_is_valid_file_complicated_schema(repo):
dummy_layout_rule = pack.create_layout_rule(
"MyRule",
{
"rule_id": "test_rule",
"id": "test_rule",
"layout_id": "test_layout_id",
"description": "This trigger is test",
"rule_name": "test rule name",
"name": "test rule name",
"alerts_filter": {
"filter": {
"OR": [
Expand Down Expand Up @@ -109,10 +109,10 @@ def test_is_not_valid_file_complicated_schema(repo):
dummy_layout_rule = pack.create_layout_rule(
"MyRule",
{
"rule_id": "test_rule",
"id": "test_rule",
"layout_id": "test_layout_id",
"description": "This trigger is test",
"rule_name": "test rule name",
"name": "test rule name",
"alerts_filter": {
"filter": {
"OR": [
Expand Down
7 changes: 6 additions & 1 deletion demisto_sdk/commands/validate/sdk_validation_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,12 @@ select = [
"LO107",
"VC100",
"VC101",
"BA112"
"BA112",
"XT101",
"LO101",
"XR101",
"TR101",
"XD101"
]
warning = [
"GR109",
Expand Down
58 changes: 58 additions & 0 deletions demisto_sdk/commands/validate/tests/TR_validators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from demisto_sdk.commands.validate.validators.TR_validators.TR100_is_silent_trigger import (
IsSilentTriggerValidator,
)
from demisto_sdk.commands.validate.validators.TR_validators.TR101_standardized_fields import (
TriggerStandardizedFieldsValidator,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -74,3 +77,58 @@ def test_IsSilentTriggerValidator(name, is_silent, result_len, file_name):
[trigger]
)
assert result_len == len(invalid_content_items)


class TestTriggerStandardizedFieldsValidator:
def test_valid_trigger_with_standard_fields(self):
"""Test that trigger with standard 'id' and 'name' fields passes validation."""
trigger = create_trigger_object()
trigger.data.update({"id": "test-trigger-id", "name": "Test Trigger Name"})

validator = TriggerStandardizedFieldsValidator()
results = validator.obtain_invalid_content_items([trigger])

assert len(results) == 0

def test_invalid_trigger_with_old_id_field_only(self):
"""Test that trigger with only 'trigger_id' fails validation."""
trigger = create_trigger_object()
trigger.data.update(
{"trigger_id": "old-trigger-id", "name": "Test Trigger Name"}
)

validator = TriggerStandardizedFieldsValidator()
results = validator.obtain_invalid_content_items([trigger])

assert len(results) == 1
assert results[0].validator.error_code == "TR101"

def test_invalid_trigger_with_old_name_field_only(self):
"""Test that trigger with only 'trigger_name' fails validation."""
trigger = create_trigger_object()
trigger.data.update(
{"id": "test-trigger-id", "trigger_name": "Old Trigger Name"}
)

validator = TriggerStandardizedFieldsValidator()
results = validator.obtain_invalid_content_items([trigger])

assert len(results) == 1
assert results[0].validator.error_code == "TR101"

def test_valid_trigger_with_both_fields(self):
"""Test that trigger with both old and new fields passes validation."""
trigger = create_trigger_object()
trigger.data.update(
{
"id": "test-trigger-id",
"trigger_id": "old-trigger-id", # Backward compatibility
"name": "Test Trigger Name",
"trigger_name": "Old Trigger Name", # Backward compatibility
}
)

validator = TriggerStandardizedFieldsValidator()
results = validator.obtain_invalid_content_items([trigger])

assert len(results) == 0
Loading