Skip to content

Commit

Permalink
Merge pull request #22 from alanisaac/fix-merged-category-validation
Browse files Browse the repository at this point in the history
Fixed validation to not merge dependencies into categories
  • Loading branch information
alanisaac authored Apr 30, 2024
2 parents f9dad48 + 091dc38 commit 9e91094
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ocsf_validator/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ def __init__(self):

def get_type(self):
return OcsfCategories

class ExcludeMatcher(Matcher):
"""
A matcher that produces the opposite result of the matcher it's given.
"""
def __init__(self, matcher: Matcher):
self.matcher = matcher

def match(self, value: str) -> bool:
return not self.matcher.match(value)
12 changes: 11 additions & 1 deletion ocsf_validator/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from typing import Any, Callable, Optional

from ocsf_validator.errors import *
from ocsf_validator.matchers import (
CategoriesMatcher,
ExcludeMatcher
)
from ocsf_validator.reader import Reader
from ocsf_validator.type_mapping import TypeMapping
from ocsf_validator.types import (
Expand Down Expand Up @@ -462,7 +466,13 @@ def process_includes(
fulfilled: set[str] = set()
dependencies = Dependencies()

for path in reader.match():
# categories cannot be extended with dependencies, and it causes problems
# if we try to include dictionary attributes in categories
matcher = ExcludeMatcher(
CategoriesMatcher()
)

for path in reader.match(matcher):
for directive, parser in parsers.items():
if parser.found_in(path):
for target in parser.extract_targets(path):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def test_extension_matcher():
assert m.get_type() is OcsfExtension


def test_exclude_matcher():
m = ExcludeMatcher(
ExtensionMatcher()
)

assert m.match("/extensions/ext1/extension.json") is False
assert m.match("/extension.json") is True


def test_make_matcher():
m = Matcher.make(".*thing.json")

Expand Down

0 comments on commit 9e91094

Please sign in to comment.