Python jsonschema.exceptions.best_match() Examples

The following are 30 code examples of jsonschema.exceptions.best_match(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jsonschema.exceptions , or try the search function .
Example #1
Source File: test_exceptions.py    From misp42splunk with GNU Lesser General Public License v3.0 7 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 #2
Source File: test_exceptions.py    From core with 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 #3
Source File: test_exceptions.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_if_the_most_relevant_error_is_allOf_it_is_traversed(self):
        """
        Now, if the error is allOf, we traverse but select the *most* relevant
        error from the context, because all schemas here must match anyways.

        """

        validator = Draft4Validator(
            {
                "properties": {
                    "foo": {
                        "allOf": [
                            {"type": "string"},
                            {"properties": {"bar": {"type": "array"}}},
                        ],
                    },
                },
            },
        )
        best = self.best_match(validator.iter_errors({"foo": {"bar": 12}}))
        self.assertEqual(best.validator_value, "string") 
Example #4
Source File: test_exceptions.py    From misp42splunk with 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 #5
Source File: test_exceptions.py    From Requester with 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 #6
Source File: test_exceptions.py    From pyblish-qml with 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 #7
Source File: test_exceptions.py    From pyblish-qml with 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 #8
Source File: test_exceptions.py    From pyblish-qml with 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 #9
Source File: test_exceptions.py    From pyblish-qml with 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 #10
Source File: test_exceptions.py    From misp42splunk with 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 #11
Source File: test_exceptions.py    From misp42splunk with 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 #12
Source File: test_exceptions.py    From misp42splunk with 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 #13
Source File: test_exceptions.py    From accelerated-data-lake with 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 #14
Source File: test_exceptions.py    From accelerated-data-lake with 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 #15
Source File: test_exceptions.py    From accelerated-data-lake with 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 #16
Source File: test_exceptions.py    From core with 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 #17
Source File: test_exceptions.py    From accelerated-data-lake with 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 #18
Source File: test_exceptions.py    From core with 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 #19
Source File: test_exceptions.py    From core with 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 #20
Source File: test_exceptions.py    From Requester with 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 #21
Source File: test_exceptions.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 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 #22
Source File: test_exceptions.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 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 #23
Source File: test_exceptions.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 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 #24
Source File: test_exceptions.py    From SA-ctf_scoreboard with Creative Commons Zero v1.0 Universal 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 #25
Source File: test_exceptions.py    From Requester with 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 #26
Source File: test_exceptions.py    From deepWordBug with 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 #27
Source File: test_exceptions.py    From deepWordBug with 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 #28
Source File: test_exceptions.py    From deepWordBug with 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 #29
Source File: test_exceptions.py    From deepWordBug with 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 #30
Source File: core.py    From notifiers with MIT License 6 votes vote down vote up
def _validate_data(self, data: dict):
        """
        Validates data against provider schema. Raises :class:`~notifiers.exceptions.BadArguments` if relevant

        :param data: Data to validate
        :raises: :class:`~notifiers.exceptions.BadArguments`
        """
        log.debug("validating provided data")
        e = best_match(self.validator.iter_errors(data))
        if e:
            custom_error_key = f"error_{e.validator}"
            msg = (
                e.schema[custom_error_key]
                if e.schema.get(custom_error_key)
                else e.message
            )
            raise BadArguments(validation_error=msg, provider=self.name, data=data)