Exemplo n.º 1
0
    def __unicode__(self):
        essential_for_verbose = (
            self.validator,
            self.validator_value,
            self.instance,
            self.schema,
        )
        if any(m is _unset for m in essential_for_verbose):
            return self.message

        pschema = pprint.pformat(self.schema, width=72)
        pinstance = pprint.pformat(self.instance, width=72)
        return self.message + textwrap.dedent("""

            Failed validating %r in %s %s:
            %s

            On %s %s:
            %s
            """.rstrip()) % (
            self.validator,
            self._word_for_schema_in_error_message,
            _utils.format_as_index(list(self.relative_schema_path)[:-1]),
            _utils.indent(pschema),
            self._word_for_instance_in_error_message,
            _utils.format_as_index(self.relative_path),
            _utils.indent(pinstance),
        )
Exemplo n.º 2
0
    def __unicode__(self):
        essential_for_verbose = (
            self.validator, self.validator_value, self.instance, self.schema,
        )
        if any(m is _unset for m in essential_for_verbose):
            return self.message

        pschema = pprint.pformat(self.schema, width=72)
        pinstance = pprint.pformat(self.instance, width=72)
        return self.message + textwrap.dedent("""

            Failed validating %r in %s%s:
            %s

            On %s%s:
            %s
            """.rstrip()
        ) % (
            self.validator,
            self._word_for_schema_in_error_message,
            _utils.format_as_index(list(self.relative_schema_path)[:-1]),
            _utils.indent(pschema),
            self._word_for_instance_in_error_message,
            _utils.format_as_index(self.relative_path),
            _utils.indent(pinstance),
        )
Exemplo n.º 3
0
    def __unicode__(self):
        essential_for_verbose = (
            self.validator,
            self.validator_value,
            self.instance,
            self.schema,
        )
        if any(m is _unset for m in essential_for_verbose):
            return self.message

        self.schema = yaml.dump(
            json.loads(json.dumps(self.schema, sort_keys=True, indent=4)))
        self.instance = yaml.dump(
            json.loads(json.dumps(self.instance, sort_keys=True, indent=4)))

        pschema = yaml.dump(self.schema, default_flow_style=False)
        pinstance = yaml.dump(self.instance, default_flow_style=False)

        return self.message + textwrap.dedent("""

            Failed validating %s at %s:
            %s

            By %r validator in %s at %s:
            %s
            """.rstrip()) % (
            self._word_for_instance_in_error_message,
            _utils.format_as_index(self.relative_path),
            pinstance,
            self.validator,
            self._word_for_schema_in_error_message,
            _utils.format_as_index(list(self.relative_schema_path)[:-1]),
            pschema,
        )
Exemplo n.º 4
0
    def __unicode__(self):
        if _unset in (
            self.validator, self.validator_value, self.instance, self.schema,
        ):
            return self.message

        path = _utils.format_as_index(self.path)
        schema_path = _utils.format_as_index(list(self.schema_path)[:-1])

        pschema = pprint.pformat(self.schema, width=72)
        pinstance = pprint.pformat(self.instance, width=72)
        return self.message + textwrap.dedent("""

            Failed validating %r in schema%s:
            %s

            On instance%s:
            %s
            """.rstrip()
        ) % (
            self.validator,
            schema_path,
            _utils.indent(pschema),
            path,
            _utils.indent(pinstance),
        )
Exemplo n.º 5
0
    def __str__(self):
        essential_for_verbose = (
            self.validator,
            self.validator_value,
            self.instance,
            self.schema,
        )
        if any(m is _unset for m in essential_for_verbose):
            return self.message

        schema_path = _utils.format_as_index(
            container=self._word_for_schema_in_error_message,
            indices=list(self.relative_schema_path)[:-1],
        )
        instance_path = _utils.format_as_index(
            container=self._word_for_instance_in_error_message,
            indices=self.relative_path,
        )
        prefix = 16 * " "

        return dedent(
            f"""\
            {self.message}

            Failed validating {self.validator!r} in {schema_path}:
                {indent(pformat(self.schema, width=72), prefix).lstrip()}

            On {instance_path}:
                {indent(pformat(self.instance, width=72), prefix).lstrip()}
            """.rstrip(), )
Exemplo n.º 6
0
def _pprint_validation_error(error):
    """
    A version of jsonschema's ValidationError __str__ method that doesn't
    include the schema fragment that failed.  This makes the error messages
    much more succinct.

    It also shows any subschemas of anyOf/allOf that failed, if any (what
    jsonschema calls "context").
    """
    essential_for_verbose = (
        error.validator,
        error.validator_value,
        error.instance,
        error.schema,
    )
    if any(m is _unset for m in essential_for_verbose):
        return error.message

    pinstance = pprint.pformat(error.instance, width=72)

    parts = [
        'On {}{}:'.format(
            error._word_for_instance_in_error_message,
            _utils.format_as_index(error.relative_path),
        ),
        _utils.indent(pinstance), '', error.message
    ]
    if error.context:
        parts.extend(_utils.indent(x.message) for x in error.context)

    return '\n'.join(parts)
Exemplo n.º 7
0
    def validate_schema(self, data_file, file_base):
        return_value = True

        schema_file = self.get_schema_path(file_base)

        try:
            schema_contents = CPConfigFile.parse(schema_file)
            data_contents = CPConfigFile.parse(data_file)
            # We've already checked the input files for errors, but not the
            # schema files
            schema_errors = CPConfigFile.errors(schema_file)
            schema_warnings = CPConfigFile.warnings(schema_file)
            if schema_warnings:
                self._warnings += schema_warnings
            if schema_errors:
                self._errors += schema_errors
                return False
        except Exception as e:
            msg = (
                'Syntax errors detected, data:"%s" schema "%s" errors "%s" ' %
                (data_file, schema_file, e))
            self.add_error(msg)
            return_value = False

        try:
            schema_check = validators.validator_for(schema_contents)
            schema_check.check_schema(schema_contents)
        except SchemaError as e:
            self.add_error('Schema "%s" is invalid: %s' % (schema_contents, e))
            return False

        try:
            validate = Draft4Validator(schema_contents)
            error_list = [err for err in validate.iter_errors(data_contents)]
            if len(error_list) > 0:
                error_string = '\n\nInput\n%s\n\nCould not be validated - list of errors:\n' % (
                    _utils.indent(
                        yaml.dump(data_contents,
                                  default_flow_style=False,
                                  indent=4)))
                for e in error_list:
                    error_string += "%s\n%s\n%s\n" % (
                        _utils.indent("Index of error:       %s" %
                                      _utils.format_as_index(deque(e.path))),
                        _utils.indent("    Erroneous value:     %s" %
                                      pprint.pformat(e.instance, width=72)),
                        _utils.indent(
                            "    Expected type:       %s" % e.validator_value))
                self.add_error(error_string)
                return_value = False

        except Exception as e:
            self.add_error('File "%s" or "%s" could not be loaded: %s' %
                           (schema_file, data_file, e))
            return_value = False

        return return_value
    def validate_schema(self, data_file, file_base):
        return_value = True

        schema_file = self.get_schema_path(file_base)

        try:
            schema_contents = CPConfigFile.parse(schema_file)
            data_contents = CPConfigFile.parse(data_file)
            # We've already checked the input files for errors, but not the
            # schema files
            schema_errors = CPConfigFile.errors(schema_file)
            schema_warnings = CPConfigFile.warnings(schema_file)
            if schema_warnings:
                self._warnings += schema_warnings
            if schema_errors:
                self._errors += schema_errors
                return False
        except Exception as e:
            msg = ('Syntax errors detected, data:"%s" schema "%s" errors "%s" '
                   % (data_file, schema_file, e))
            self.add_error(msg)
            return_value = False

        try:
            schema_check = validators.validator_for(schema_contents)
            schema_check.check_schema(schema_contents)
        except SchemaError as e:
            self.add_error('Schema "%s" is invalid: %s' % (
                schema_contents, e))
            return False

        try:
            validate = Draft4Validator(schema_contents)
            error_list = [err for err in validate.iter_errors(data_contents)]
            if len(error_list) > 0:
                error_string = '\n\nInput\n%s\n\nCould not be validated - list of errors:\n' % (
                    _utils.indent(yaml.dump(data_contents, default_flow_style=False, indent=4)))
                for e in error_list:
                    error_string += "%s\n%s\n%s\n" % (
                        _utils.indent("Index of error:       %s" %
                                      _utils.format_as_index(deque(e.path))),
                        _utils.indent("    Erroneous value:     %s" %
                                      pprint.pformat(e.instance, width=72)),
                        _utils.indent("    Expected type:       %s" %
                                      e.validator_value))
                self.add_error(error_string)
                return_value = False

        except Exception as e:
            self.add_error('File "%s" or "%s" could not be loaded: %s' % (
                schema_file, data_file, e))
            return_value = False

        return return_value
Exemplo n.º 9
0
    def _validate(self, config):
        try:
            anyconfig.validate(config, self.schema, ac_schema_safe=False)
        except jsonschema.exceptions.ValidationError as e:
            schema_error = "Failed validating '{validator}' in schema{schema}\n{message}".format(
                validator=e.validator,
                schema=format_as_index(list(e.relative_schema_path)[:-1]),
                message=e.message)
            raise ansibledoctor.exception.ConfigError("Configuration error",
                                                      schema_error)

        return True
Exemplo n.º 10
0
 def _validate(self, config):
     try:
         anyconfig.validate(config, self.schema, ac_schema_safe=False)
         return True
     except jsonschema.exceptions.ValidationError as e:
         schema_error = (
             "Error while loading configuration:\n"
             "Failed validating '{validator}' in schema{schema}").format(
                 validator=e.validator,
                 schema=format_as_index(list(e.relative_schema_path)[:-1]))
         utils.sysexit_with_message("{schema}: {msg}".format(
             schema=schema_error, msg=e.message))
Exemplo n.º 11
0
def input_schema_valid(params, schema, validator=Draft7Validator):
    """
    Determine whether the input parameters conform to the format according to the customized schema
    Args:
        params (dict): Input parameters
        schema (dict): Customized schema
    Returns:
        result (list[string]): Error message that does not conform to the schema
    """
    result = []
    v = validator(schema)
    for error in sorted(v.iter_errors(params), key=str):
        err_msg = schema_utils.format_as_index(
            error.relative_path) + ' : ' + error.message
        result.append(err_msg)
    return result