How to use the sdv.errors function in sdv

To help you get started, we’ve selected a few sdv examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github STIXProject / stix-validator / sdv / scripts / stix-validator.py View on Github external
# Validate input documents
        results = scripts.run_validation(options)

        # Print validation results
        scripts.print_results(results, options)

        # Determine exit status code and exit.
        code = scripts.status_code(results)
        sys.exit(code)

    except scripts.ArgumentError as ex:
        if ex.show_help:
            parser.print_help()
        scripts.error(ex)
    except (errors.ValidationError, IOError) as ex:
        scripts.error(
            "Validation error occurred: '%s'" % str(ex),
            codes.EXIT_VALIDATION_ERROR
        )
    except Exception:
        logging.exception("Fatal error occurred")
        sys.exit(codes.EXIT_FAILURE)
github oasis-open / cti-stix-slider / stix2slider / __init__.py View on Github external
if stix_package:
        xml = stix_package.to_xml(encoding=None)
        validator_options.in_files = io.StringIO(xml)

        try:
            scripts.set_output_level(validator_options)

            validation_results = scripts.validate_file(
                validator_options.in_files,
                validator_options
            )
            results = {stix_package.id_: validation_results}

            # Print stix-validator results
            scripts.print_results(results, validator_options)
        except (errors.ValidationError, IOError) as ex:
            scripts.error(
                "Validation error occurred: '%s'" % str(ex),
                codes.EXIT_VALIDATION_ERROR
            )
        except Exception:
            log.exception("Fatal error occurred", extra={'ecode': 0})
            sys.exit(codes.EXIT_FAILURE)

        return xml
github oasis-open / cti-stix-slider / stix2slider / __init__.py View on Github external
if stix_package:
        xml = stix_package.to_xml(encoding=None)
        validator_options.in_files = io.StringIO(xml)

        try:
            scripts.set_output_level(validator_options)

            validation_results = scripts.validate_file(
                validator_options.in_files,
                validator_options
            )
            results = {stix_package.id_: validation_results}

            # Print stix-validator results
            scripts.print_results(results, validator_options)
        except (errors.ValidationError, IOError) as ex:
            scripts.error(
                "Validation error occurred: '%s'" % str(ex),
                codes.EXIT_VALIDATION_ERROR
            )
        except Exception:
            log.exception("Fatal error occurred", extra={'ecode': 0})
            sys.exit(codes.EXIT_FAILURE)

        return xml
github STIXProject / stix-validator / sdv / scripts / cybox-validator.py View on Github external
# Validate input documents
        results = scripts.run_validation(options)

        # Print validation results
        scripts.print_results(results, options)

        # Determine exit status code and exit.
        code = scripts.status_code(results)
        sys.exit(code)

    except scripts.ArgumentError as ex:
        if ex.show_help:
            parser.print_help()
        scripts.error(ex)
    except (errors.ValidationError, IOError) as ex:
        scripts.error(
            "Validation error occurred: '%s'" % str(ex),
            codes.EXIT_VALIDATION_ERROR
        )
    except Exception:
        logging.exception("Fatal error occurred")
        sys.exit(codes.EXIT_FAILURE)
github STIXProject / stix-validator / sdv / validators / xml_schema.py View on Github external
Returns:
            An instance of
            :class:`.XmlValidationResults`.

        Raises:
            .ValidationError: If the class was not initialized with a
                schema directory and `schemaloc` is ``False`` or if there are
                any issues parsing `doc`.
            .XMLSchemaIncludeError: If an error occurs while processing the
                schemas required for validation.
            .XMLSchemaIncludeError: If an error occurs while processing
                ``xs:include`` directives.

        """
        if not (schemaloc or self._schemalocs):
            raise errors.ValidationError(
                "No schemas to validate against! Try instantiating "
                "XmlValidator with use_schemaloc=True or setting the "
                "schema_dir param in __init__"
            )

        root = utils.get_etree_root(doc)
        xsd = self._build_uber_schema(root, schemaloc)
        is_valid = xsd.validate(root)

        return XmlValidationResults(is_valid, xsd.error_log)
github STIXProject / stix-validator / sdv / validators / stix / profile.py View on Github external
def validate(self):
        """Checks that this is a valid InstanceMapping instance.

        Raises:
            errors.ProfileParseError: If ``namespace`` is ``None`` or
                any of the selector values are empty.

        """
        if not self.label:
            err = "Missing type label in Instance Mapping"
            raise errors.ProfileParseError(err)

        if not self.namespace:
            err = "Missing namespace for '{label}'' in Instance Mapping worksheet"
            raise errors.ProfileParseError(err.format(label=self.label))

        if not (self.selectors and all(self.selectors)):
            err = ("Empty selector for '{label}' in Instance Mapping worksheet. "
                   "Look for extra commas in field.")
            raise errors.ProfileParseError(err.format(label=self.label))
github STIXProject / stix-validator / sdv / validators / cybox / schema.py View on Github external
def _raise_invalid_version(self, version):
        error = (
            "Invalid CybOX version number provided or found in input "
            "document: '{0}'"
        ).format(version)

        raise errors.InvalidCyboxVersionError(
            message=error,
            found=version,
            expected=common.CYBOX_VERSIONS
        )
github STIXProject / stix-validator / sdv / validators / xml_schema.py View on Github external
    @errors.setter
    def errors(self, value):
        if not value:
            self._errors = []
        elif utils.is_iterable(value):
            self._errors = [XmlSchemaError(x) for x in value]
        else:
            self._errors = [XmlSchemaError(value)]
github STIXProject / stix-validator / sdv / validators / xml_schema.py View on Github external
initialization schema directory is used.

        Returns:
            An ``etree.XMLSchema`` instance used to validate `doc`.

        Raise:
            .XMLSchemaImportError: If an error occurred while building the
                dictionary of namespace to schemalocation mappings used to
                drive the uber schema creation.

        """
        root = utils.get_etree_root(doc)
        imports = self._build_required_imports(root, schemaloc)

        if not imports:
            raise errors.XMLSchemaImportError(
                "Cannot validate document. Error occurred while determining "
                "schemas required for validation."
            )

        xsd = etree.fromstring(
            """
            
            """
        )

        for ns, loc in iteritems(imports):
            loc = loc.replace("\\", "/")