Skip to content

partial_object_validate

trestle.core.commands.partial_object_validate ¤

Trestle schema-validate command.

Attributes¤

logger = logging.getLogger(__name__) module-attribute ¤

Classes¤

PartialObjectValidate ¤

Bases: CommandBase

Direct validation any oscal object in a file, including list objects.

Source code in trestle/core/commands/partial_object_validate.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class PartialObjectValidate(CommandBase):
    """Direct validation any oscal object in a file, including list objects."""

    name = 'partial-object-validate'

    def _init_arguments(self) -> None:
        self.add_argument(
            f'-{const.ARG_FILE_SHORT}',
            f'--{const.ARG_FILE}',
            help=const.ARG_DESC_FILE + ' to validate',
            required=True,
            type=pathlib.Path
        )

        self.add_argument(
            f'-{const.ARG_ELEMENT_SHORT}',
            f'--{const.ARG_ELEMENT}',
            help=const.ARG_DESC_ELEMENT + ' to validate.',
            required=True
        )

        self.add_argument(
            '-nv', '--no-validators', help='Only perform the most basic validation of the file', action='store_true'
        )

    def _run(self, args: argparse.Namespace) -> int:
        try:
            log.set_log_level_from_args(args)
            file_path: pathlib.Path = args.file.resolve()
            if not file_path.exists() or not file_path.is_file():
                raise TrestleError('File path provided does not exist or is a directory')

            element_str: str = args.element
            if ',' in element_str:
                logger.warning('Only a single element path is allowed.')

            return self.partial_object_validate(file_path, element_str)
        except Exception as e:  # pragma: no cover
            return handle_generic_command_exception(e, logger, 'Error while validating OSCAL file')

    @classmethod
    def partial_object_validate(cls, file_path: pathlib.Path, element_string: str) -> int:
        """Run a schema validation on a file inferring file type based on element string."""
        # get model type
        logger.info(f'Validating {file_path}')
        element_path = elements.ElementPath(element_string)
        # get a wrapped object
        obm_type = element_path.get_obm_wrapped_type()
        obm_type.oscal_read(file_path)

        logger.info(f'VALID: {file_path} for {element_string}')
        return CmdReturnCodes.SUCCESS.value
Attributes¤
name = 'partial-object-validate' class-attribute instance-attribute ¤
Functions¤
partial_object_validate(file_path, element_string) classmethod ¤

Run a schema validation on a file inferring file type based on element string.

Source code in trestle/core/commands/partial_object_validate.py
72
73
74
75
76
77
78
79
80
81
82
83
@classmethod
def partial_object_validate(cls, file_path: pathlib.Path, element_string: str) -> int:
    """Run a schema validation on a file inferring file type based on element string."""
    # get model type
    logger.info(f'Validating {file_path}')
    element_path = elements.ElementPath(element_string)
    # get a wrapped object
    obm_type = element_path.get_obm_wrapped_type()
    obm_type.oscal_read(file_path)

    logger.info(f'VALID: {file_path} for {element_string}')
    return CmdReturnCodes.SUCCESS.value

Functions¤

handler: python