Skip to content

partial_object_validate

trestle.core.commands.partial_object_validate ¤

Trestle schema-validate command.

logger ¤

Classes¤

PartialObjectValidate (CommandBase) ¤

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

Source code in trestle/core/commands/partial_object_validate.py
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
name ¤
Methods¤
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
@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

handler: python