Skip to content

Commit bf7ee6e

Browse files
author
Bryan Worrell
committed
Ignore whitespace when checking for content in fields with idrefs.
Fixes #50.
1 parent 3c926e3 commit bf7ee6e

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

sdv/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,31 @@ def has_tzinfo(timestamp):
295295
"""
296296
ts = parse_timestamp(timestamp)
297297
return ts and bool(ts.tzinfo)
298+
299+
300+
def strip_whitespace(string):
301+
"""Returns a copy of `string` with all whitespace removed.
302+
303+
"""
304+
if string is None:
305+
return None
306+
307+
return ''.join(string.split())
308+
309+
310+
def has_content(node):
311+
"""Returns ``True`` if the `node` has children or text nodes.
312+
313+
Note:
314+
This will ignore whitespace and XML comments.
315+
316+
"""
317+
if node is None:
318+
return False
319+
320+
if len(node.findall('*')) > 0:
321+
return True
322+
323+
stripped = strip_whitespace(node.text)
324+
return bool(stripped)
325+

sdv/validators/stix/best_practice.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ def _check_idref_with_content(self, root, namespaces, version): # noqa
425425
def is_invalid(node):
426426
if common.is_idref_content_exception(node):
427427
return False
428-
return bool(node.text) or len(node.findall('*')) > 0
428+
429+
return utils.has_content(node)
429430

430431
nodes = root.xpath("//*[@idref]")
431432
warnings = [BestPracticeWarning(x) for x in nodes if is_invalid(x)]
@@ -575,8 +576,6 @@ def _check_timestamp_usage(self, root, namespaces, **kwargs): # noqa
575576
warning['timestamp'] = timestamp
576577
results.append(warning)
577578

578-
warning = None # overwritten below
579-
580579
if id_ and not timestamp:
581580
warning = BestPracticeWarning(
582581
node=node,

0 commit comments

Comments
 (0)