Skip to content

Commit

Permalink
fix: make _validate_xml actuall return a bool in all cases
Browse files Browse the repository at this point in the history
Co-authored-by: Terri Oda <terri.oda@intel.com>
  • Loading branch information
devesh-2002 and terriko committed Mar 21, 2024
1 parent d81ca11 commit d3c608f
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions cve_bin_tool/validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (C) 2022 Anthony Harrison
# SPDX-License-Identifier: GPL-3.0-or-later

import logging
from pathlib import Path

Expand All @@ -13,8 +12,9 @@


def _validate_xml(filename, xsd_file):
# Resolve folder where schemas are present
"""
Validates an XML file against a specified XSD schema.
The XSD schema file contains the 'grammar rules' that the XML file should follow.
It first constructs the path to the XSD schema file, then it creates an XMLSchema object using the xmlschema library.
It logs a debug message about the validation process, then attempts to validate the XML file against the schema.
Expand All @@ -29,16 +29,16 @@ def _validate_xml(filename, xsd_file):
Returns:
bool: True if the XML file is valid according to the schema, False otherwise.
"""

# Resolve the folder where schemas are located.
schemas_file = Path(__file__).resolve().parent / "schemas" / xsd_file
the_schema = xmlschema.XMLSchema(Path(schemas_file))

LOGGER.debug(f"Validate {filename} against the_schema in {schemas_file}")
LOGGER.debug(f"Validating {filename} against the schema in {schemas_file}")
try:
result = the_schema.validate(filename)
except Exception as e:
LOGGER.debug(f"Failed to validate {filename} against {xsd_file}. Exception {e}")
result = "Fail"
result = False
return result is None


Expand All @@ -54,7 +54,6 @@ def validate_spdx(filename):
Returns:
bool: True if the SPDX file is valid according to the schema, False otherwise.
"""

SPDX_SCHEMA = "spdx.xsd"
return _validate_xml(filename, SPDX_SCHEMA)

Expand Down

0 comments on commit d3c608f

Please sign in to comment.