Python jsonschema.Draft4Validator() Examples

The following are code examples for showing how to use jsonschema.Draft4Validator(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: drydock   Author: airshipit   File: deckhand.py    Apache License 2.0 6 votes vote down vote up
def validate_drydock_document(self, doc):
        """Validate a parsed document via jsonschema.

        If a schema for a document Kind is not available, the document is
        considered valid. Schema is chosen by the doc['kind'] field.

        Returns a empty list for valid documents, otherwise returns a list
        of all found errors

        :param doc: dictionary of the parsed document.
        """
        schemaname = doc.get('schema', '')
        (schema_ns, doc_kind, doc_version) = schemaname.split('/')

        errors_found = []

        if doc_version == 'v1':
            if schemaname in self.v1_doc_schemas:
                validator = jsonschema.Draft4Validator(
                    self.v1_doc_schemas.get(schemaname))
                for error in validator.iter_errors(doc.get('data', [])):
                    errors_found.append(error.message)

        return errors_found 
Example 2
Project: drydock   Author: airshipit   File: yaml.py    Apache License 2.0 6 votes vote down vote up
def validate_drydock_document(self, doc):
        """Validate a parsed document via jsonschema.

        If a schema for a document Kind is not available, the document is
        considered valid. Schema is chosen by the doc['kind'] field.

        Returns a empty list for valid documents, otherwise returns a list
        of all found errors

        :param doc: dictionary of the parsed document.
        """
        doc_kind = doc.get('kind')
        if doc_kind in self.v1_doc_schemas:
            validator = jsonschema.Draft4Validator(
                self.v1_doc_schemas.get(doc_kind))
            errors_found = []
            for error in validator.iter_errors(doc):
                errors_found.append(error.message)
            return errors_found
        else:
            return [] 
Example 3
Project: Paradrop   Author: ParadropLabs   File: chute.py    Apache License 2.0 6 votes vote down vote up
def validate(ctx):
    """
    Validate the paradrop.yaml file.

    A note about versions: this command validates the chute configuration
    against the current rules for the installed version of pdtools. If
    the chute is to be installed on a Paradrop node running a different
    version, then this command may not be reliable for determining
    compatibility.
    """
    with open('paradrop.yaml', 'r') as source:
        chute = yaml.safe_load(source)

    schema_path = pkg_resources.resource_filename('pdtools', 'schemas/chute.json')
    with open(schema_path, 'r') as source:
        schema = json.load(source)

    validator = jsonschema.Draft4Validator(schema)
    for error in sorted(validator.iter_errors(chute), key=str):
        click.echo(error.message) 
Example 4
Project: singer-tools   Author: singer-io   File: check_tap.py    Apache License 2.0 6 votes vote down vote up
def add(self, message):
        if isinstance(message, singer.RecordMessage):
            stream = self.ensure_stream(message.stream)
            if stream.latest_schema:
                validator_fn = extend_with_default(Draft4Validator)
                validator = validator_fn(
                    stream.latest_schema, format_checker=FormatChecker())
                validator.validate(copy.deepcopy(message.record))
            else:
                print('I saw a record for stream {} before the schema'.format(
                    message.stream))
                exit(1)
            stream.num_records += 1

        elif isinstance(message, singer.SchemaMessage):
            stream = self.ensure_stream(message.stream)
            stream.num_schemas += 1
            stream.latest_schema = message.schema

        elif isinstance(message, singer.StateMessage):
            self.latest_state = message.value
            self.num_states += 1 
Example 5
Project: misp42splunk   Author: remg427   File: test_exceptions.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_oneOf_and_anyOf_are_weak_matches(self):
        """
        A property you *must* match is probably better than one you have to
        match a part of.

        """

        validator = Draft4Validator(
            {
                "minProperties" : 2,
                "anyOf" : [{"type" : "string"}, {"type" : "number"}],
                "oneOf" : [{"type" : "string"}, {"type" : "number"}],
            }
        )
        best = self.best_match(validator.iter_errors({}))
        self.assertEqual(best.validator, "minProperties") 
Example 6
Project: misp42splunk   Author: remg427   File: test_exceptions.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_if_the_most_relevant_error_is_anyOf_it_is_traversed(self):
        """
        If the most relevant error is an anyOf, then we traverse its context
        and select the otherwise *least* relevant error, since in this case
        that means the most specific, deep, error inside the instance.

        I.e. since only one of the schemas must match, we look for the most
        relevant one.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "anyOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 7
Project: misp42splunk   Author: remg427   File: test_exceptions.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_if_the_most_relevant_error_is_oneOf_it_is_traversed(self):
        """
        If the most relevant error is an oneOf, then we traverse its context
        and select the otherwise *least* relevant error, since in this case
        that means the most specific, deep, error inside the instance.

        I.e. since only one of the schemas must match, we look for the most
        relevant one.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 8
Project: misp42splunk   Author: remg427   File: test_exceptions.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_nested_context_for_oneOf(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {
                                "oneOf" : [
                                    {"type" : "string"},
                                    {
                                        "properties" : {
                                            "bar" : {"type" : "array"}
                                        },
                                    },
                                ],
                            },
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 9
Project: core   Author: getavalon   File: test_exceptions.py    MIT License 6 votes vote down vote up
def test_oneOf_and_anyOf_are_weak_matches(self):
        """
        A property you *must* match is probably better than one you have to
        match a part of.

        """

        validator = Draft4Validator(
            {
                "minProperties" : 2,
                "anyOf" : [{"type" : "string"}, {"type" : "number"}],
                "oneOf" : [{"type" : "string"}, {"type" : "number"}],
            }
        )
        best = self.best_match(validator.iter_errors({}))
        self.assertEqual(best.validator, "minProperties") 
Example 10
Project: core   Author: getavalon   File: test_exceptions.py    MIT License 6 votes vote down vote up
def test_if_the_most_relevant_error_is_anyOf_it_is_traversed(self):
        """
        If the most relevant error is an anyOf, then we traverse its context
        and select the otherwise *least* relevant error, since in this case
        that means the most specific, deep, error inside the instance.

        I.e. since only one of the schemas must match, we look for the most
        relevant one.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "anyOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 11
Project: core   Author: getavalon   File: test_exceptions.py    MIT License 6 votes vote down vote up
def test_if_the_most_relevant_error_is_oneOf_it_is_traversed(self):
        """
        If the most relevant error is an oneOf, then we traverse its context
        and select the otherwise *least* relevant error, since in this case
        that means the most specific, deep, error inside the instance.

        I.e. since only one of the schemas must match, we look for the most
        relevant one.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 12
Project: core   Author: getavalon   File: test_exceptions.py    MIT License 6 votes vote down vote up
def test_nested_context_for_oneOf(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {
                                "oneOf" : [
                                    {"type" : "string"},
                                    {
                                        "properties" : {
                                            "bar" : {"type" : "array"}
                                        },
                                    },
                                ],
                            },
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 13
Project: scrapy-cluster   Author: istresearch   File: kafka_monitor.py    MIT License 6 votes vote down vote up
def setup(self, level=None, log_file=None, json=None):
        '''
        Load everything up. Note that any arg here will override both
        default and custom settings

        @param level: the log level
        @param log_file: boolean t/f whether to log to a file, else stdout
        @param json: boolean t/f whether to write the logs in json
        '''
        self.settings = self.wrapper.load(self.settings_name)

        my_level = level if level else self.settings['LOG_LEVEL']
        # negate because logger wants True for std out
        my_output = not log_file if log_file else self.settings['LOG_STDOUT']
        my_json = json if json else self.settings['LOG_JSON']
        self.logger = LogFactory.get_instance(json=my_json, stdout=my_output,
                                              level=my_level,
                                              name=self.settings['LOGGER_NAME'],
                                              dir=self.settings['LOG_DIR'],
                                              file=self.settings['LOG_FILE'],
                                              bytes=self.settings['LOG_MAX_BYTES'],
                                              backups=self.settings['LOG_BACKUPS'])

        self.validator = self.extend_with_default(Draft4Validator) 
Example 14
Project: git_for_splunk   Author: ChrisYounger   File: test_exceptions.py    Apache License 2.0 6 votes vote down vote up
def test_oneOf_and_anyOf_are_weak_matches(self):
        """
        A property you *must* match is probably better than one you have to
        match a part of.

        """

        validator = Draft4Validator(
            {
                "minProperties" : 2,
                "anyOf" : [{"type" : "string"}, {"type" : "number"}],
                "oneOf" : [{"type" : "string"}, {"type" : "number"}],
            }
        )
        best = self.best_match(validator.iter_errors({}))
        self.assertEqual(best.validator, "minProperties") 
Example 15
Project: git_for_splunk   Author: ChrisYounger   File: test_exceptions.py    Apache License 2.0 6 votes vote down vote up
def test_if_the_most_relevant_error_is_anyOf_it_is_traversed(self):
        """
        If the most relevant error is an anyOf, then we traverse its context
        and select the otherwise *least* relevant error, since in this case
        that means the most specific, deep, error inside the instance.

        I.e. since only one of the schemas must match, we look for the most
        relevant one.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "anyOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 16
Project: git_for_splunk   Author: ChrisYounger   File: test_exceptions.py    Apache License 2.0 6 votes vote down vote up
def test_if_the_most_relevant_error_is_oneOf_it_is_traversed(self):
        """
        If the most relevant error is an oneOf, then we traverse its context
        and select the otherwise *least* relevant error, since in this case
        that means the most specific, deep, error inside the instance.

        I.e. since only one of the schemas must match, we look for the most
        relevant one.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 17
Project: git_for_splunk   Author: ChrisYounger   File: test_exceptions.py    Apache License 2.0 6 votes vote down vote up
def test_nested_context_for_oneOf(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {
                                "oneOf" : [
                                    {"type" : "string"},
                                    {
                                        "properties" : {
                                            "bar" : {"type" : "array"}
                                        },
                                    },
                                ],
                            },
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 18
Project: schemas   Author: belbio   File: test_examples.py    Apache License 2.0 6 votes vote down vote up
def main():

    test_examples()
    quit()

    import json
    import pprint

    with open('../schemas/test2.yaml', 'r') as f:
        schema = yaml.load(f)

    v = jsonschema.Draft4Validator(schema)
    errors = sorted(v.iter_errors(example), key=lambda e: e.path)
    for error in errors:
        for suberror in sorted(error.context, key=lambda e: e.schema_path):
            print(list(suberror.schema_path), suberror.message, sep=", ")

    # validate(example, schema) 
Example 19
Project: python-sdk   Author: optimizely   File: validator.py    Apache License 2.0 6 votes vote down vote up
def is_datafile_valid(datafile):
    """ Given a datafile determine if it is valid or not.

  Args:
    datafile: JSON string representing the project.

  Returns:
    Boolean depending upon whether datafile is valid or not.
  """

    try:
        datafile_json = json.loads(datafile)
    except:
        return False

    try:
        jsonschema.Draft4Validator(constants.JSON_SCHEMA).validate(datafile_json)
    except:
        return False

    return True 
Example 20
Project: freqtrade   Author: Hooker41   File: configuration.py    GNU General Public License v3.0 6 votes vote down vote up
def _validate_config(self, conf: Dict[str, Any]) -> Dict[str, Any]:
        """
        Validate the configuration follow the Config Schema
        :param conf: Config in JSON format
        :return: Returns the config if valid, otherwise throw an exception
        """
        try:
            validate(conf, constants.CONF_SCHEMA)
            return conf
        except ValidationError as exception:
            logger.fatal(
                'Invalid configuration. See config.json.example. Reason: %s',
                exception
            )
            raise ValidationError(
                best_match(Draft4Validator(constants.CONF_SCHEMA).iter_errors(conf)).message
            ) 
Example 21
Project: cornerwise   Author: codeforboston   File: validate_importers.py    MIT License 6 votes vote down vote up
def handle(self, *args, **options):
        err = lambda m: self.stderr.write(self.style.ERROR(m) + "\n")
        succ = lambda m: self.stdout.write(self.style.SUCCESS(m) + "\n")
        when = datetime.now() - timedelta(days=30)
        validator = Draft4Validator(get_importer_schema())

        for importer in Importer.objects.all():
            errors = defaultdict(list)
            for error in validator.iter_errors(importer.updated_since(when)):
                errors[schema_path_str(error.absolute_schema_path)].append(error)

            if errors:
                err(f"{len(errors)} error(s) in {importer}:")
                for path, errs in errors.items():
                    error = errs[0]
                    repeats = f" ({len(errs)} repeats)" if len(errs) > 1 else ""
                    err(f"at {path}{repeats}:\n{error.message}\n")
            else:
                succ(f"{importer} validated successfully!") 
Example 22
Project: acceptable   Author: canonical-ols   File: test_djangoutils.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_errors(self, form, data):
        schema = djangoutil.get_form_schema(form)
        validator = Draft4Validator(schema, format_checker=FormatChecker())

        # manipulate jsonschema errors to look like django form error dict
        schema_errors = {}
        for error in validator.iter_errors(data):
            if error.path:
                # strip out 'foo.n' naming for arrays to just 'foo'
                key = '.'.join(
                    i for i in error.path if isinstance(i, (str, bytes))
                )
            else:
                # missing required fields are not keyed by path
                key = error.validator_value[0]
            schema_errors[key] = error.message
        return form(data=data).errors, schema_errors 
Example 23
Project: pyblish-rpc   Author: pyblish   File: test_exceptions.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_oneOf_and_anyOf_are_weak_matches(self):
        """
        A property you *must* match is probably better than one you have to
        match a part of.

        """

        validator = Draft4Validator(
            {
                "minProperties" : 2,
                "anyOf" : [{"type" : "string"}, {"type" : "number"}],
                "oneOf" : [{"type" : "string"}, {"type" : "number"}],
            }
        )
        best = self.best_match(validator.iter_errors({}))
        self.assertEqual(best.validator, "minProperties") 
Example 24
Project: pyblish-rpc   Author: pyblish   File: test_exceptions.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_if_the_most_relevant_error_is_anyOf_it_is_traversed(self):
        """
        If the most relevant error is an anyOf, then we traverse its context
        and select the otherwise *least* relevant error, since in this case
        that means the most specific, deep, error inside the instance.

        I.e. since only one of the schemas must match, we look for the most
        relevant one.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "anyOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 25
Project: pyblish-rpc   Author: pyblish   File: test_exceptions.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_if_the_most_relevant_error_is_oneOf_it_is_traversed(self):
        """
        If the most relevant error is an oneOf, then we traverse its context
        and select the otherwise *least* relevant error, since in this case
        that means the most specific, deep, error inside the instance.

        I.e. since only one of the schemas must match, we look for the most
        relevant one.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 26
Project: pyblish-rpc   Author: pyblish   File: test_exceptions.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_nested_context_for_oneOf(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {
                                "oneOf" : [
                                    {"type" : "string"},
                                    {
                                        "properties" : {
                                            "bar" : {"type" : "array"}
                                        },
                                    },
                                ],
                            },
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : 12}}))
        self.assertEqual(best.validator_value, "array") 
Example 27
Project: cadasta-platform   Author: Cadasta   File: validators.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def validate_json(value, schema):
    v = Draft4Validator(schema, format_checker=FormatChecker())
    errors = sorted(v.iter_errors(value), key=lambda e: e.path)

    message_dict = {}
    for e in errors:
        if e.validator == 'anyOf':
            fields = [
                f.message.split(' ')[0].replace('\'', '') for f in e.context
            ]

            for f in fields:
                message_dict[f] = _("Please provide either {}").format(
                    _(" or ").join(fields))

        elif e.validator == 'required':
            field = e.message.split(' ')[0].replace('\'', '')
            message_dict[field] = _("This field is required.")
        else:
            field = '.'.join([str(el) for el in e.path])
            message_dict[field] = e.message

    if message_dict:
        raise JsonValidationError(message_dict) 
Example 28
Project: python-pubsub   Author: Superbalist   File: validator.py    MIT License 6 votes vote down vote up
def validate_message(self, message):
        try:
            message.get('schema', None)
        except ValueError:
            raise ValueError('Message must contain a schema!')
        except AttributeError:
            raise AttributeError('Message must be json')
        schema_uri = message.get('schema', '')
        matches = re.findall(r'(.+)://(.+/)?(.+)\.json', schema_uri)
        if len(matches) < 1:
            raise SchemaValidationError('Incorrect schema uri: {}'.format(schema_uri))
        schema = self.resolver.resolve_from_url(schema_uri)

        errors = []
        for error in Draft4Validator(schema, resolver=self.resolver).iter_errors(message):
            errors.append(error)
        if errors:
            raise ValidationError(errors=errors) 
Example 29
Project: swaggerit   Author: dutradda   File: utils.py    MIT License 5 votes vote down vote up
def build_validator(schema, path):
    handlers = {'': _URISchemaHandler(path)}
    resolver = RefResolver.from_schema(schema, handlers=handlers)
    return Draft4Validator(schema, resolver=resolver) 
Example 30
Project: pydatajson   Author: datosgobar   File: validation.py    MIT License 5 votes vote down vote up
def init_jsonschema_validator(self, schema_dir, schema_filename):
        schema_path = os.path.join(schema_dir, schema_filename)
        schema = readers.read_json(schema_path)
        # SegĂșn https://github.com/Julian/jsonschema/issues/98
        # Permite resolver referencias locales a otros esquemas.
        if platform.system() == 'Windows':
            base_uri = "file:///" + schema_path.replace("\\", "/")
        else:
            base_uri = "file://" + schema_path
        resolver = jsonschema.RefResolver(base_uri=base_uri, referrer=schema)
        format_checker = jsonschema.FormatChecker()
        return jsonschema.Draft4Validator(
            schema=schema, resolver=resolver, format_checker=format_checker) 
Example 31
Project: misp42splunk   Author: remg427   File: test_cli.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_find_validator_in_jsonschema(self):
        arguments = cli.parse_args(
            [
                "--validator", "Draft4Validator",
                "--instance", "foo.json",
                "schema.json",
            ]
        )
        self.assertIs(arguments["validator"], Draft4Validator) 
Example 32
Project: misp42splunk   Author: remg427   File: test_exceptions.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_shallower_errors_are_better_matches(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "minProperties" : 2,
                        "properties" : {"bar" : {"type" : "object"}},
                    }
                }
            }
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : []}}))
        self.assertEqual(best.validator, "minProperties") 
Example 33
Project: misp42splunk   Author: remg427   File: test_exceptions.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_one_error(self):
        validator = Draft4Validator({"minProperties" : 2})
        error, = validator.iter_errors({})
        self.assertEqual(
            exceptions.best_match(validator.iter_errors({})).validator,
            "minProperties",
        ) 
Example 34
Project: misp42splunk   Author: remg427   File: test_exceptions.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_no_errors(self):
        validator = Draft4Validator({})
        self.assertIsNone(exceptions.best_match(validator.iter_errors({}))) 
Example 35
Project: core   Author: getavalon   File: test_cli.py    MIT License 5 votes vote down vote up
def test_find_validator_in_jsonschema(self):
        arguments = cli.parse_args(
            [
                "--validator", "Draft4Validator",
                "--instance", "foo.json",
                "schema.json",
            ]
        )
        self.assertIs(arguments["validator"], Draft4Validator) 
Example 36
Project: core   Author: getavalon   File: test_exceptions.py    MIT License 5 votes vote down vote up
def test_shallower_errors_are_better_matches(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "minProperties" : 2,
                        "properties" : {"bar" : {"type" : "object"}},
                    }
                }
            }
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : []}}))
        self.assertEqual(best.validator, "minProperties") 
Example 37
Project: core   Author: getavalon   File: test_exceptions.py    MIT License 5 votes vote down vote up
def test_one_error(self):
        validator = Draft4Validator({"minProperties" : 2})
        error, = validator.iter_errors({})
        self.assertEqual(
            exceptions.best_match(validator.iter_errors({})).validator,
            "minProperties",
        ) 
Example 38
Project: core   Author: getavalon   File: test_exceptions.py    MIT License 5 votes vote down vote up
def test_no_errors(self):
        validator = Draft4Validator({})
        self.assertIsNone(exceptions.best_match(validator.iter_errors({}))) 
Example 39
Project: scrapy-cluster   Author: istresearch   File: rest_service.py    MIT License 5 votes vote down vote up
def __init__(self, settings_name):
        """
        @param settings_name: the local settings file name
        """
        self.settings_name = settings_name
        self.wrapper = SettingsWrapper()
        self.logger = None
        self.app = Flask(__name__)
        self.kafka_connected = False
        self.redis_connected = False
        self.my_uuid = str(uuid.uuid4()).split('-')[4]
        self.uuids = {}
        self.uuids_lock = threading.Lock()
        self.validator = self._extend_with_default(Draft4Validator)
        self.schemas = {} 
Example 40
Project: git_for_splunk   Author: ChrisYounger   File: test_cli.py    Apache License 2.0 5 votes vote down vote up
def test_find_validator_in_jsonschema(self):
        arguments = cli.parse_args(
            [
                "--validator", "Draft4Validator",
                "--instance", "foo.json",
                "schema.json",
            ]
        )
        self.assertIs(arguments["validator"], Draft4Validator) 
Example 41
Project: git_for_splunk   Author: ChrisYounger   File: test_exceptions.py    Apache License 2.0 5 votes vote down vote up
def test_shallower_errors_are_better_matches(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "minProperties" : 2,
                        "properties" : {"bar" : {"type" : "object"}},
                    }
                }
            }
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : []}}))
        self.assertEqual(best.validator, "minProperties") 
Example 42
Project: git_for_splunk   Author: ChrisYounger   File: test_exceptions.py    Apache License 2.0 5 votes vote down vote up
def test_one_error(self):
        validator = Draft4Validator({"minProperties" : 2})
        error, = validator.iter_errors({})
        self.assertEqual(
            exceptions.best_match(validator.iter_errors({})).validator,
            "minProperties",
        ) 
Example 43
Project: git_for_splunk   Author: ChrisYounger   File: test_exceptions.py    Apache License 2.0 5 votes vote down vote up
def test_no_errors(self):
        validator = Draft4Validator({})
        self.assertIsNone(exceptions.best_match(validator.iter_errors({}))) 
Example 44
Project: schemas   Author: belbio   File: check_json.py    Apache License 2.0 5 votes vote down vote up
def validate_to_schema(data, schema: Mapping[str, Any]) -> Tuple[bool, List[Tuple[str, str]]]:
    """Validate datafile against jsonschema

    Args:
        data: datafile to validate
        schema (Mapping[str, Any]): schema

    Returns:
        Tuple[bool, List[str]]:
            bool: Is valid?  Yes = True, No = False
            List[Tuple[str, str]]: Validation issues, empty if valid, tuple is ('Error|Warning', msg)
                e.g. [('ERROR', "'subject' is a required property")]
    """

    v = jsonschema.Draft4Validator(schema)
    messages = []
    errors = sorted(v.iter_errors(data), key=lambda e: e.path)
    for error in errors:
        for suberror in sorted(error.context, key=lambda e: e.schema_path):
            print(list(suberror.schema_path), suberror.message, '\n\n', sep=", ")
            messages.append(('ERROR', suberror.message))

    is_valid = True
    if errors:
        is_valid = False

    return (is_valid, messages) 
Example 45
Project: schemas   Author: belbio   File: check_nanopub_array.py    Apache License 2.0 5 votes vote down vote up
def validate_to_schema(data, schema: Mapping[str, Any]) -> Tuple[bool, List[Tuple[str, str]]]:
    """Validate datafile against jsonschema

    Args:
        data: datafile to validate
        schema (Mapping[str, Any]): schema

    Returns:
        Tuple[bool, List[str]]:
            bool: Is valid?  Yes = True, No = False
            List[Tuple[str, str]]: Validation issues, empty if valid, tuple is ('Error|Warning', msg)
                e.g. [('ERROR', "'subject' is a required property")]
    """

    v = jsonschema.Draft4Validator(schema)
    messages = []
    errors = sorted(v.iter_errors(data), key=lambda e: e.path)
    for error in errors:
        for suberror in sorted(error.context, key=lambda e: e.schema_path):
            print(list(suberror.schema_path), suberror.message, '\n\n', sep=", ")
            messages.append(('ERROR', suberror.message))

    is_valid = True
    if errors:
        is_valid = False

    return (is_valid, messages) 
Example 46
Project: schemas   Author: belbio   File: test_examples.py    Apache License 2.0 5 votes vote down vote up
def test_examples():
    """Check that schemas are valid"""

    examples = glob.glob(f'{home_path}/examples/*.yaml')

    for fn in examples:
        print('Example fn: ', fn)
        basename = os.path.basename(fn)

        with open(fn, 'r') as f:
            example = yaml.load(f)

        schema_basename = re.sub('\-.*?\-', '-', basename)
        schema_fn = f'{home_path}/schemas/{schema_basename}'
        print('Schema FN', schema_fn)

        with open(schema_fn, 'r') as f:
            schema = yaml.load(f)

        v = jsonschema.Draft4Validator(schema)
        errors = sorted(v.iter_errors(example), key=lambda e: e.path)
        for error in errors:
            for suberror in sorted(error.context, key=lambda e: e.schema_path):
                print(list(suberror.schema_path), suberror.message, sep=", ")

        if errors:
            assert False

        # except Exception as e:
        #     print('Problem with example: ', fn)
        #     print(e) 
Example 47
Project: inspire-schemas   Author: inspirehep   File: utils.py    GNU General Public License v2.0 5 votes vote down vote up
def get_validation_errors(data, schema=None):
    """Validation errors for a given record.

    Args:
        data (dict): record to validate.
        schema (Union[dict, str]): schema to validate against. If it is a
            string, it is intepreted as the name of the schema to load (e.g.
            ``authors`` or ``jobs``). If it is ``None``, the schema is taken
            from ``data['$schema']``. If it is a dictionary, it is used
            directly.
    Yields:
        jsonschema.exceptions.ValidationError: validation errors.

    Raises:
        SchemaNotFound: if the given schema was not found.
        SchemaKeyNotFound: if ``schema`` is ``None`` and no ``$schema`` key was
            found in ``data``.
        jsonschema.SchemaError: if the schema is invalid.
    """
    schema = _load_schema_for_record(data, schema)

    errors = Draft4Validator(
        schema,
        resolver=LocalRefResolver.from_schema(schema),
        format_checker=inspire_format_checker
    )
    return errors.iter_errors(data) 
Example 48
Project: mappyfile   Author: geographika   File: validator.py    MIT License 5 votes vote down vote up
def get_schema_validator(self, schema_name):
        """
        Had to remove the id property from map.json or it uses URLs for validation
        See various issues at https://github.com/Julian/jsonschema/pull/306
        """

        if schema_name not in self.schemas:
            schema_file = self.get_schema_file(schema_name)
            with open(schema_file) as f:
                try:
                    jsn_schema = json.load(f)
                except ValueError as ex:
                    log.error("Could not load %s", schema_file)
                    raise ex

            schemas_folder = self.get_schemas_folder()
            root_schema_path = self.get_schema_path(schemas_folder)
            resolver = jsonschema.RefResolver(root_schema_path, None)
            # cache the schema for future use
            self.schemas[schema_name] = (jsn_schema, resolver)
        else:
            jsn_schema, resolver = self.schemas[schema_name]

        validator = jsonschema.Draft4Validator(schema=jsn_schema, resolver=resolver)
        # validator.check_schema(jsn_schema) # check schema is valid

        return validator 
Example 49
Project: sheepdog   Author: uc-cdis   File: manifest.py    Apache License 2.0 5 votes vote down vote up
def validate_upload_manifest(manifest, schema=None):
    """Generate a list of errors found in JSON Schema validation."""
    if schema is None:
        schema = UPLOAD_MANIFEST_SCHEMA
    return [e.message for e in Draft4Validator(schema).iter_errors(manifest)] 
Example 50
Project: aiida-lammps   Author: abelcarreras   File: __init__.py    MIT License 5 votes vote down vote up
def validate_with_json(data, name):
    """ validate json-type data against a schema

    :param data: dictionary
    :param name: <name>.schema.json
    """
    schema = read_schema(name)
    validator = jsonschema.Draft4Validator

    # by default, only validates lists
    validator(schema, types={"array": (list, tuple)}).validate(data) 
Example 51
Project: aiida-lammps   Author: abelcarreras   File: __init__.py    MIT License 5 votes vote down vote up
def validate_with_dict(data, schema):
    """ validate json-type data against a schema

    :param data: dictionary
    :param schema: dictionary
    """
    validator = jsonschema.Draft4Validator

    # by default, only validates lists
    validator(schema, types={"array": (list, tuple)}).validate(data) 
Example 52
Project: pib   Author: datawire   File: schema.py    Apache License 2.0 5 votes vote down vote up
def validate(schema, instance):
    """
    Validate a YAML file that has been parsed into a Python object.

    :param schema: The decoded (POPO) schema.
    :param instance: The decoded (POPO) instance to validate.

    :raises ValidationError: if validation failed.
    """
    validator = Draft4Validator(schema)
    if not validator.is_valid(instance):
        errors = []
        for e in validator.iter_errors(instance):
            message = e.message
            # Better error messages, workaround for
            # https://github.com/Julian/jsonschema/issues/316:
            if (e.validator == "additionalProperties" and
                    not e.validator_value and "patternProperties" in e.schema):
                for key in e.instance.keys():
                    if repr(key) in e.message:
                        message = (
                            "{} does not match any of these "
                            "regexs: {}.".format(
                                repr(key),
                                ", ".join(e.schema["patternProperties"].keys())))
                        break
            errors.append(
                "/{}: {}".format("/".join(map(str, e.path)), message))
        raise ValidationError(errors) 
Example 53
Project: asclepias-broker   Author: asclepias   File: api.py    MIT License 5 votes vote down vote up
def _jsonschema_validator_func():
    schema_host = current_app.config['JSONSCHEMAS_HOST']
    schema_store = {
        f'{schema_host}/scholix-v3.json': SCHOLIX_SCHEMA,
        f'{schema_host}/event.json': EVENT_SCHEMA,
    }
    resolver = jsonschema.RefResolver(
        schema_host, EVENT_SCHEMA, schema_store)
    return jsonschema.Draft4Validator(EVENT_SCHEMA, resolver=resolver) 
Example 54
Project: Metis   Author: gusibi   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema):
        self.validator = Draft4Validator(schema) 
Example 55
Project: Metis   Author: gusibi   File: validators.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, schema):
        self.validator = Draft4Validator(schema) 
Example 56
Project: sdtd   Author: ratilicus   File: schemaforms.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, data, *args, **kwargs):
        self.cleaned_data = {}
        self.errors = []

        # get only the fields in the schema and flatten where specified
        self.data = {}
        for k, v in self.schema['properties'].items():
            if k in data:
                kv = data[k]
                if isinstance(kv, (list, tuple)) and k in self.flatten:
                    self.data[k] = kv[0]
                else:
                    self.data[k] = kv
            elif 'default' in v:
                self.data[k] = v['default']

        # validate data based on schema
        validator = Draft4Validator(self.schema)
        self.errors = [
            ((e.message.split("'", 2)[1], u'Required Field')
             if e.validator == 'required' else
             ('.'.join(e.path), e.message))
            for e in validator.iter_errors(self.data)]

        if not self.errors:
            self.cleaned_data = self.clean(self.data, data, *args, **kwargs) 
Example 57
Project: url_shortener   Author: martydill   File: validator.py    MIT License 5 votes vote down vote up
def get_validator(version=None, version_minor=None):
    """Load the JSON schema into a Validator"""
    if version is None:
        from .. import current_nbformat
        version = current_nbformat

    v = import_item("IPython.nbformat.v%s" % version)
    current_minor = v.nbformat_minor
    if version_minor is None:
        version_minor = current_minor

    version_tuple = (version, version_minor)

    if version_tuple not in validators:
        try:
            v.nbformat_schema
        except AttributeError:
            # no validator
            return None
        schema_path = os.path.join(os.path.dirname(v.__file__), v.nbformat_schema)
        with open(schema_path) as f:
            schema_json = json.load(f)

        if current_minor < version_minor:
            # notebook from the future, relax all `additionalProperties: False` requirements
            schema_json = _relax_additional_properties(schema_json)
            # and allow undefined cell types and outputs
            schema_json = _allow_undefined(schema_json)

        validators[version_tuple] = Validator(schema_json)
    return validators[version_tuple] 
Example 58
Project: target-stitch   Author: singer-io   File: __init__.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def handle_batch(self, messages, contains_activate_version, schema, key_names, bookmark_names=None, state_writer=None, state=None): # pylint: disable=no-self-use,unused-argument
        '''Handles messages by validating them against schema.'''
        LOGGER.info("ValidatingHandler handle_batch")
        validator = Draft4Validator(schema, format_checker=FormatChecker())
        for i, message in enumerate(messages):
            if isinstance(message, singer.RecordMessage):
                try:
                    validator.validate(message.record)
                    if key_names:
                        for k in key_names:
                            if k not in message.record:
                                raise TargetStitchException(
                                    'Message {} is missing key property {}'.format(
                                        i, k))
                except Exception as e:
                    raise TargetStitchException(
                        'Record does not pass schema validation: {}'.format(e))

        # pylint: disable=undefined-loop-variable
        # NB: This seems incorrect as there's a chance message is not defined
        LOGGER.info('%s (%s): Batch is valid',
                    messages[0].stream,
                    len(messages))
        if state:
            line = simplejson.dumps(state)
            state_writer.write("{}\n".format(line))
            state_writer.flush() 
Example 59
Project: notifications-api   Author: alphagov   File: __init__.py    MIT License 5 votes vote down vote up
def validate(json_to_validate, schema):
    validator = Draft4Validator(schema)
    validator.validate(json_to_validate, schema) 
Example 60
Project: doctor   Author: upsight   File: schema.py    MIT License 5 votes vote down vote up
def get_validator(self, schema=None):
        """Get a jsonschema validator.

        :param dict schema: A custom schema to validate against.
        :returns: an instance of jsonschema Draft4Validator.
        """
        schema = schema if schema is not None else self.schema
        return jsonschema.Draft4Validator(
            schema, resolver=self.resolver,
            format_checker=jsonschema.draft4_format_checker) 
Example 61
Project: doctor   Author: upsight   File: test_schema.py    MIT License 5 votes vote down vote up
def test_get_validator(self):
        validator = self.schema.get_validator()
        assert isinstance(validator, jsonschema.Draft4Validator)
        assert validator.resolver == self.schema.resolver
        assert validator.schema == self.schema.schema 
Example 62
Project: acceptable   Author: canonical-ols   File: _validation.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def validate(payload, schema):
    """Validate `payload` against `schema`, returning an error list.

    jsonschema provides lots of information in it's errors, but it can be a bit
    of work to extract all the information.
    """
    v = jsonschema.Draft4Validator(
        schema, format_checker=jsonschema.FormatChecker())
    error_list = []
    for error in v.iter_errors(payload):
        message = error.message
        location = '/' + '/'.join([str(c) for c in error.absolute_path])
        error_list.append(message + ' at ' + location)
    return error_list 
Example 63
Project: acceptable   Author: canonical-ols   File: _validation.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def validate_schema(schema):
    """Validate that 'schema' is correct.

    This validates against the jsonschema v4 draft.

    :raises jsonschema.SchemaError: if the schema is invalid.
    """
    jsonschema.Draft4Validator.check_schema(schema) 
Example 64
Project: Django-Mongo-Boilerplate   Author: shreybatra   File: request_validation.py    MIT License 5 votes vote down vote up
def validate_json_body(body, schema):
    '''This method is used to validate request body against defined schema.

    Args:
        body: JSON like dictionary request body
        schema: JSON like dictionary schema for sent request body

    Returns:
        List of errors against each key in json request body

        Example:
            [
                {
                    3 is not of type 'string',
                    {} is not of type 'string'
                }
            ]
    '''

    errors = []
    validator = Draft4Validator(schema)

    for error in sorted(validator.iter_errors(body)):
        errors.append(error.message)

    return errors if errors else None 
Example 65
Project: cjio   Author: cityjson   File: validation.py    MIT License 5 votes vote down vote up
def validate_against_schema(j, js):
    isValid = True
    es = []
    #-- lazy validation to catch as many as possible
    myvalidator = jsonschema.Draft4Validator(js, format_checker=jsonschema.FormatChecker())
    for err in sorted(myvalidator.iter_errors(j), key=str):
        isValid = False
        es.append(err.message)
    return (isValid, es)

    # try:
    #     jsonschema.Draft4Validator(js, format_checker=jsonschema.FormatChecker()).validate(j)
    # except jsonschema.ValidationError as e:
    #     raise Exception(e.message)
    #     return False
    # except jsonschema.SchemaError as e:
    #     raise Exception(e.message)
    #     return False
    
    # try:
    #     jsonschema.validate(j, js, format_checker=jsonschema.FormatChecker())
    # except jsonschema.ValidationError as e:
    #     raise Exception(e.message)
    #     return False
    # except jsonschema.SchemaError as e:
    #     raise Exception(e.message)
    #     return False 
Example 66
Project: pyblish-rpc   Author: pyblish   File: test_cli.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_find_validator_in_jsonschema(self):
        arguments = cli.parse_args(
            [
                "--validator", "Draft4Validator",
                "--instance", "foo.json",
                "schema.json",
            ]
        )
        self.assertIs(arguments["validator"], Draft4Validator) 
Example 67
Project: pyblish-rpc   Author: pyblish   File: test_exceptions.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_shallower_errors_are_better_matches(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "minProperties" : 2,
                        "properties" : {"bar" : {"type" : "object"}},
                    }
                }
            }
        )
        best = self.best_match(validator.iter_errors({"foo" : {"bar" : []}}))
        self.assertEqual(best.validator, "minProperties") 
Example 68
Project: pyblish-rpc   Author: pyblish   File: test_exceptions.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_one_error(self):
        validator = Draft4Validator({"minProperties" : 2})
        error, = validator.iter_errors({})
        self.assertEqual(
            exceptions.best_match(validator.iter_errors({})).validator,
            "minProperties",
        ) 
Example 69
Project: pyblish-rpc   Author: pyblish   File: test_exceptions.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_no_errors(self):
        validator = Draft4Validator({})
        self.assertIsNone(exceptions.best_match(validator.iter_errors({}))) 
Example 70
Project: paasta   Author: Yelp   File: validate.py    Apache License 2.0 5 votes vote down vote up
def validate_schema(file_path, file_type):
    """Check if the specified config file has a valid schema

    :param file_path: path to file to validate
    :param file_type: what schema type should we validate against
    """
    try:
        schema = get_schema(file_type)
    except Exception as e:
        paasta_print(f"{SCHEMA_ERROR}: {file_type}, error: {e!r}")
        return

    if schema is None:
        paasta_print(f"{SCHEMA_NOT_FOUND}: {file_path}")
        return
    validator = Draft4Validator(schema, format_checker=FormatChecker())
    basename = os.path.basename(file_path)
    config_file_object = get_config_file_dict(file_path)
    try:
        validator.validate(config_file_object)
        if file_type == "kubernetes" and not validate_instance_names(
            config_file_object, file_path
        ):
            return
    except ValidationError:
        paasta_print(f"{SCHEMA_INVALID}: {file_path}")

        errors = validator.iter_errors(config_file_object)
        paasta_print("  Validation Message: %s" % exceptions.best_match(errors).message)
    except Exception as e:
        paasta_print(f"{SCHEMA_ERROR}: {file_type}, error: {e!r}")
        return
    else:
        paasta_print(f"{SCHEMA_VALID}: {basename}")
        return True 
Example 71
Project: video-analytics-serving   Author: intel   File: PipelineManager.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_input_valid(request,pipeline_config,section):
        config = pipeline_config.get(section,{})
        try:
            if (section in request):
                input_validator = jsonschema.Draft4Validator(schema=config, format_checker=jsonschema.draft4_format_checker)
                input_validator.validate(request.get(section, {}))    
                logger.debug("{} Validation successful".format(section))
            return True
        except Exception as error:
            logger.debug("Validation error in request section {} payload".format(section))
            return False 
Example 72
Project: scrapy-jsonschema   Author: scrapy-plugins   File: test_item.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_get_validator(self):
        schema = {
            "$schema": JSON_SCHEMA_DRAFT_7,
            "title": "Item with Schema Draft",
        }

        draft7_validator = JsonSchemaItem._get_validator(schema)
        self.assertTrue(isinstance(draft7_validator, Draft7Validator))

        no_draft_chema = {"title": "Item without schema Draft"}
        default_validator = JsonSchemaItem._get_validator(no_draft_chema)
        self.assertTrue(isinstance(default_validator, Draft4Validator)) 
Example 73
Project: tozti   Author: tozti   File: utils.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def validate(inst, schema):
    """Validate data against a JsonSchema."""

    return jsonschema.validate(inst, schema, cls=jsonschema.Draft4Validator,
                               format_checker=jsonschema.FormatChecker()) 
Example 74
Project: stash-scanner   Author: senuido   File: ItemFilter.py    GNU General Public License v3.0 5 votes vote down vote up
def init(cls):
        try:
            with open(Filter.FILTER_SCHEMA_FNAME) as f:
                schema = json.load(f)

            jsonschema.validate({}, schema)
        except jsonschema.ValidationError:
            cls.schema_validator = jsonschema.Draft4Validator(schema)
        except jsonschema.SchemaError as e:
            raise AppException('Failed loading filter validation schema.\n{}'.format(FILTER_SCHEMA_ERROR.format(e)))
        # except FileNotFoundError as e:
        except Exception as e:
            raise AppException('Failed loading filter validation schema.\n{}\n'
                               'Make sure the file are valid and in place.'.format(e)) 
Example 75
Project: pastebin2misp   Author: renzejongman   File: test_cli.py    MIT License 5 votes vote down vote up
def test_find_validator_in_jsonschema(self):
        arguments = cli.parse_args(
            [
                "--validator", "Draft4Validator",
                "--instance", "foo.json",
                "schema.json",
            ]
        )
        self.assertIs(arguments["validator"], Draft4Validator) 
Example 76
Project: YODA-File   Author: ODM2   File: display_file.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def df_validate(args ):

    level = args.level

    validation_log =["validation starting"]
    stream = file(args.source)

    ymal_data = yaml.load (stream)

    validation_log.append("level 0: File valid JSON or YAML")
    if (level > 0 ):
        schema = file(args.schema)
        json_schema = json.load(schema)
        validation_log.append("Starting Level 2")
        try:
            v = Draft4Validator(json_schema)
            errors = v.iter_errors(ymal_data)
            # building an ErrorTree fails.
            # Means we can't use ErrorTree to get a count or provide more details.
            #   possibly to do  Space in KeyName?.
            #tree = ErrorTree(errors)
            #tree = ErrorTree(v.iter_errors(ymal_data))
            errorList =  list(sorted(errors, key=lambda e: e.path))

           #if (tree.total_errors==0 ) :
            if (len(errorList) == 0):
                logger.info('Level 2 validation; File valid to Display File Structural Specification')
                return

            #for error in tree.errors:
            for error in errorList:
                logger.info ('in iterated errors')
                logger.warn (  error.message + ' in ' +
                "/".join( [ str(element) for element in error.path ] )
                )
        except ValidationError as e:
            logger.info ('in validation error')
            logger.warn(e.message)
        except SchemaError as e:
            logger.error(e)


    pass 
Example 77
Project: pcocc   Author: cea-hpc   File: Networks.py    GNU General Public License v3.0 4 votes vote down vote up
def load(self, filename):
        """Loads the network config

        Instantiates a dict holding a VNetwork class for each configured
        network

        """
        try:
            stream = file(filename, 'r')
            net_config = yaml.safe_load(stream)
        except yaml.YAMLError as err:
            raise InvalidConfigurationError(str(err))
        except IOError as err:
            raise InvalidConfigurationError(str(err))

        try:
            jsonschema.Draft4Validator(VNetwork.schema).validate(net_config)
        except jsonschema.exceptions.ValidationError as err:
            type_errs = []

            #Top level error in a network name
            if not err.context:
                raise InvalidConfigurationError(err.message)

            # Iterate over all context errors in the anyof schemas
            for e in sorted(err.context, key=lambda e: e.schema_path):
                # Check for network type error
                if (len(e.schema_path) == 4 and e.schema_path[1] == 'properties' and
                    e.schema_path[2] ==  'type' and e.schema_path[3] == 'enum'):
                    type_errs.append(e)

            # Errors relevant to the requested network type
            rel_errs = [ e for e in err.context if e.schema_path[0] not in
                         [ t.schema_path[0] for t in type_errs ] ]

            # No relevant network type: not among valid network types
            if not rel_errs:
                raise InvalidConfigurationError('\n'.join([e.message for e in type_errs]))

            # Most significant error among requested network type
            best_err = jsonschema.exceptions.best_match(rel_errs)
            raise InvalidConfigurationError(best_err.message)

        for name, net_attr in net_config.iteritems():
            self[name] = VNetwork.create(net_attr['type'],
                                         name,
                                         net_attr['settings']) 
Example 78
Project: scrapy-cluster   Author: istresearch   File: test_kafka_monitor.py    MIT License 4 votes vote down vote up
def test_process_messages(self):
        self.kafka_monitor.consumer = MagicMock()
        self.kafka_monitor.stats_dict = {}

        # handle kafka offset errors
        self.kafka_monitor.consumer = MagicMock(
                        side_effect=OffsetOutOfRangeError("1"))
        try:
            self.kafka_monitor._process_messages()
        except OffsetOutOfRangeError:
            self.fail("_process_messages did not handle Kafka Offset Error")

        # handle bad json errors
        message_string = "{\"sdasdf   sd}"

        # fake class so we can use dot notation
        class a(object):
            pass

        m = a()
        m.value = message_string
        messages = [m]

        self.kafka_monitor.consumer = MagicMock()
        self.kafka_monitor.consumer.__iter__.return_value = messages
        try:
            self.kafka_monitor._process_messages()
        except OffsetOutOfRangeError:
            self.fail("_process_messages did not handle bad json")

        # set up to process messages
        self.kafka_monitor._load_plugins()
        self.kafka_monitor.validator = self.kafka_monitor.extend_with_default(Draft4Validator)
        list(self.kafka_monitor.plugins_dict.items())[0][1]['instance'].handle = MagicMock(side_effect=AssertionError("scrape"))
        list(self.kafka_monitor.plugins_dict.items())[1][1]['instance'].handle = MagicMock(side_effect=AssertionError("action"))

        #  test that handler function is called for the scraper
        message_string = "{\"url\":\"www.stuff.com\",\"crawlid\":\"1234\"," \
            "\"appid\":\"testapp\"}"
        m.value = message_string
        messages = [m]
        self.kafka_monitor.consumer.__iter__.return_value = messages
        try:
            self.kafka_monitor._process_messages()
            self.fail("Scrape not called")
        except AssertionError as e:
            self.assertEquals("scrape", str(e))

        # test that handler function is called for the actions
        message_string = "{\"uuid\":\"blah\",\"crawlid\":\"1234\"," \
            "\"appid\":\"testapp\",\"action\":\"info\",\"spiderid\":\"link\"}"

        m.value = message_string
        messages = [m]
        self.kafka_monitor.consumer.__iter__.return_value = messages
        try:
            self.kafka_monitor._process_messages()
            self.fail("Action not called")
        except AssertionError as e:
            self.assertEquals("action", str(e)) 
Example 79
Project: schemas   Author: belbio   File: test_examples.py    Apache License 2.0 4 votes vote down vote up
def test_instances():

    test_instances = glob.glob(f'{home_path}/tests/test_instances/*.yaml')

    for fn in test_instances:

        basename = os.path.basename(fn)
        print('Fn', fn)

        with open(fn, 'r') as f:
            test_instance = yaml.load(f)

        schema_basename = re.sub('\-.*?\-', '-', basename)
        schema_fn = f'{home_path}/schemas/{schema_basename}'
        print('Schema FN', schema_fn)

        with open(schema_fn, 'r') as f:
            schema = yaml.load(f)

        good_flag = False  # whether test instance should succeed or fail
        if re.search('-good.*?-', fn):
            good_flag = True

        if good_flag:
            v = jsonschema.Draft4Validator(schema)
            errors = sorted(v.iter_errors(test_instance), key=lambda e: e.path)

            for error in errors:
                for suberror in sorted(error.context, key=lambda e: e.schema_path):
                    print(list(suberror.schema_path), suberror.message, sep=", ")

            if errors:
                assert False

            # try:
            #     validate(test_instance, schema)
            # except Exception as e:
            #     assert False
            #     print('Problem with test instance: ', fn, ' against schema: ', schema_fn)
            #     print(e)

        else:
            with pytest.raises(ValidationError, message='Expecting JSONschema validation error') as e:
                validate(test_instance, schema)
                print(e.value) 
Example 80
Project: armada   Author: airshipit   File: validate.py    Apache License 2.0 4 votes vote down vote up
def validate_armada_document(document):
    """Validates a document ingested by Armada by subjecting it to JSON schema
    validation.

    :param dict dictionary: The document to validate.

    :returns: A tuple of (bool, list[dict]) where the first value
        indicates whether the validation succeeded or failed and
        the second value is the validation details with a minimum
        keyset of (message(str), error(bool))
    :rtype: tuple.
    :raises TypeError: If ``document`` is not of type ``dict``.

    """
    if not isinstance(document, dict):
        raise TypeError(
            'The provided input "%s" must be a dictionary.' % document)

    schema = document.get('schema', '<missing>')
    document_name = document.get('metadata', {}).get('name', None)
    details = []
    LOG.debug('Validating document [%s] %s', schema, document_name)

    schema_info = sch.get_schema_info(schema)
    if schema_info:
        try:
            validator = jsonschema.Draft4Validator(schema_info.data)
            for error in validator.iter_errors(document.get('data')):
                error_message = "Invalid document [%s] %s: %s." % \
                    (schema, document_name, error.message)
                vmsg = ValidationMessage(
                    message=error_message,
                    error=True,
                    name='ARM100',
                    level='Error',
                    schema=schema,
                    doc_name=document_name)
                LOG.info('ValidationMessage: %s', vmsg.get_output_json())
                details.append(vmsg.get_output())
        except jsonschema.SchemaError as e:
            error_message = (
                'The built-in Armada JSON schema %s is invalid. '
                'Details: %s.' % (e.schema, e.message))
            vmsg = ValidationMessage(
                message=error_message,
                error=True,
                name='ARM000',
                level='Error',
                diagnostic='Armada is misconfigured.')
            LOG.error('ValidationMessage: %s', vmsg.get_output_json())
            details.append(vmsg.get_output())

    if len([x for x in details if x.get('error', False)]) > 0:
        return False, details

    return True, details