Python jsonschema.exceptions.FormatError() Examples

The following are code examples for showing how to use jsonschema.exceptions.FormatError(). 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: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: validators.py    Apache License 2.0 6 votes vote down vote up
def check(self, instance, format):
        """Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format
        """

        if format not in self.checkers:
            return

        # For safety reasons custom checkers can be registered with
        # allowed exception types. Anything else will fall into the
        # default formatter.
        func, raises = self.checkers[format]
        result, cause = None, None

        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            msg = "%r is not a %r" % (instance, format)
            raise jsonschema_exc.FormatError(msg, cause=cause) 
Example 2
Project: misp42splunk   Author: remg427   File: _format.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 3
Project: misp42splunk   Author: remg427   File: _format.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 4
Project: nova   Author: ZhanHan   File: validators.py    Apache License 2.0 6 votes vote down vote up
def check(self, instance, format):
        """Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format
        """

        if format not in self.checkers:
            return

        # For safety reasons custom checkers can be registered with
        # allowed exception types. Anything else will fall into the
        # default formatter.
        func, raises = self.checkers[format]
        result, cause = None, None

        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            msg = "%r is not a %r" % (instance, format)
            raise jsonschema_exc.FormatError(msg, cause=cause) 
Example 5
Project: git_for_splunk   Author: ChrisYounger   File: _format.py    Apache License 2.0 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 6
Project: git_for_splunk   Author: ChrisYounger   File: _format.py    Apache License 2.0 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 7
Project: stx-nova   Author: starlingx-staging   File: validators.py    Apache License 2.0 6 votes vote down vote up
def check(self, instance, format):
        """Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format
        """

        if format not in self.checkers:
            return

        # For safety reasons custom checkers can be registered with
        # allowed exception types. Anything else will fall into the
        # default formatter.
        func, raises = self.checkers[format]
        result, cause = None, None

        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            msg = "%r is not a %r" % (instance, format)
            raise jsonschema_exc.FormatError(msg, cause=cause) 
Example 8
Project: pyblish-rpc   Author: pyblish   File: _format.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 9
Project: pyblish-rpc   Author: pyblish   File: _format.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 10
Project: TA-thehive   Author: remg427   File: _format.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 11
Project: TA-thehive   Author: remg427   File: _format.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 12
Project: TA-intezer   Author: secops4thewin   File: _format.py    MIT License 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 13
Project: TA-intezer   Author: secops4thewin   File: _format.py    MIT License 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 14
Project: TA-securitytrails   Author: secops4thewin   File: _format.py    MIT License 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 15
Project: TA-securitytrails   Author: secops4thewin   File: _format.py    MIT License 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 16
Project: masakari   Author: openstack   File: validators.py    Apache License 2.0 6 votes vote down vote up
def check(self, instance, format):
        """Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format
        """

        if format not in self.checkers:
            return

        # For safety reasons custom checkers can be registered with
        # allowed exception types. Anything else will fall into the
        # default formatter.
        func, raises = self.checkers[format]
        result, cause = None, None

        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            msg = "%r is not a %r" % (instance, format)
            raise jsonschema_exc.FormatError(msg, cause=cause) 
Example 17
Project: Pizza-or-Not-a-Pizza   Author: mraza007   File: _format.py    MIT License 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 18
Project: Pizza-or-Not-a-Pizza   Author: mraza007   File: _format.py    MIT License 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 19
Project: TA-bigfix   Author: jimmyatSplunk   File: _format.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 20
Project: TA-bigfix   Author: jimmyatSplunk   File: _format.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 21
Project: SA-ctf_scoreboard   Author: splunk   File: _format.py    Creative Commons Zero v1.0 Universal 6 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :raises: :exc:`FormatError` if instance does not conform to format

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 22
Project: SA-ctf_scoreboard   Author: splunk   File: _format.py    Creative Commons Zero v1.0 Universal 6 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        :argument instance: the instance to check
        :type: any primitive type (str, number, bool)
        :argument str format: the format that instance should conform to
        :rtype: bool

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 23
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_api_validation.py    Apache License 2.0 5 votes vote down vote up
def test_format_checker_failed(self):
        format_checker = validators.FormatChecker()
        exc = self.assertRaises(jsonschema_exc.FormatError,
                                format_checker.check, "   ", "name")
        self.assertIsInstance(exc.cause, exception.InvalidName)
        self.assertEqual("An invalid 'name' value was provided. The name must "
                         "be: printable characters. "
                         "Can not start or end with whitespace.",
                         exc.cause.format_message()) 
Example 24
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_api_validation.py    Apache License 2.0 5 votes vote down vote up
def test_format_checker_failed_with_non_string(self):
        checks = ["name", "name_with_leading_trailing_spaces",
                  "cell_name", "cell_name_with_leading_trailing_spaces"]
        format_checker = validators.FormatChecker()

        for check in checks:
            exc = self.assertRaises(jsonschema_exc.FormatError,
                                    format_checker.check, None, "name")
            self.assertIsInstance(exc.cause, exception.InvalidName)
            self.assertEqual("An invalid 'name' value was provided. The name "
                             "must be: printable characters. "
                             "Can not start or end with whitespace.",
                             exc.cause.format_message()) 
Example 25
Project: misp42splunk   Author: remg427   File: _validators.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 26
Project: nova   Author: ZhanHan   File: test_api_validation.py    Apache License 2.0 5 votes vote down vote up
def test_format_checker_failed(self):
        format_checker = validators.FormatChecker()
        exc = self.assertRaises(jsonschema_exc.FormatError,
                                format_checker.check, "   ", "name")
        self.assertIsInstance(exc.cause, exception.InvalidName)
        self.assertEqual("An invalid 'name' value was provided. The name must "
                         "be: printable characters. "
                         "Can not start or end with whitespace.",
                         exc.cause.format_message()) 
Example 27
Project: nova   Author: ZhanHan   File: test_api_validation.py    Apache License 2.0 5 votes vote down vote up
def test_format_checker_failed_with_non_string(self):
        checks = ["name", "name_with_leading_trailing_spaces",
                  "cell_name", "cell_name_with_leading_trailing_spaces"]
        format_checker = validators.FormatChecker()

        for check in checks:
            exc = self.assertRaises(jsonschema_exc.FormatError,
                                    format_checker.check, None, "name")
            self.assertIsInstance(exc.cause, exception.InvalidName)
            self.assertEqual("An invalid 'name' value was provided. The name "
                             "must be: printable characters. "
                             "Can not start or end with whitespace.",
                             exc.cause.format_message()) 
Example 28
Project: git_for_splunk   Author: ChrisYounger   File: _validators.py    Apache License 2.0 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 29
Project: stx-nova   Author: starlingx-staging   File: test_api_validation.py    Apache License 2.0 5 votes vote down vote up
def _format_checker(self, format, value, error_message):
        format_checker = validators.FormatChecker()
        exc = self.assertRaises(jsonschema_exc.FormatError,
                                format_checker.check, value, format)
        self.assertIsInstance(exc.cause, exception.InvalidName)
        self.assertEqual(error_message,
                         exc.cause.format_message()) 
Example 30
Project: pyblish-rpc   Author: pyblish   File: _validators.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 31
Project: python-ngoschema   Author: numengo   File: jsonschema.py    GNU General Public License v3.0 5 votes vote down vote up
def extends_ngo_draft1(validator, extends, instance, schema):
    if validator.is_type(extends, "array"):
        for ref in extends:
            try:
                _format_checker(validator).check(ref, "uri-reference")
                scope, resolved = validator.resolver.resolve(ref)
            except FormatError as er:
                yield ValidationError(str(er), cause=er.cause)
            except RefResolutionError as er:
                yield ValidationError(str(er)) 
Example 32
Project: pastebin2misp   Author: renzejongman   File: _format.py    MIT License 5 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to


        Raises:

            :exc:`FormatError` if instance does not conform to ``format``

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 33
Project: pastebin2misp   Author: renzejongman   File: _format.py    MIT License 5 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to

        Returns:

            bool: Whether it conformed

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 34
Project: pastebin2misp   Author: renzejongman   File: _validators.py    MIT License 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 35
Project: openapi-core   Author: p1c2u   File: _format.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check(self, instance, format):
        if format not in self.checkers:
            raise FormatError(
                "Format checker for %r format not found" % (format, ))
        return super(StrictFormatChecker, self).check(
            instance, format) 
Example 36
Project: openapi-core   Author: p1c2u   File: _validators.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def format(validator, format, instance, schema):
    if instance is None:
        return

    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 37
Project: TA-thehive   Author: remg427   File: _validators.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 38
Project: accelerated-data-lake   Author: aws-samples   File: _format.py    Apache License 2.0 5 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to


        Raises:

            :exc:`FormatError` if instance does not conform to ``format``

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 39
Project: accelerated-data-lake   Author: aws-samples   File: _format.py    Apache License 2.0 5 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to

        Returns:

            bool: Whether it conformed

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 40
Project: accelerated-data-lake   Author: aws-samples   File: _validators.py    Apache License 2.0 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 41
Project: TA-intezer   Author: secops4thewin   File: _validators.py    MIT License 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 42
Project: Requester   Author: kylebebak   File: _format.py    MIT License 5 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to


        Raises:

            :exc:`FormatError` if instance does not conform to ``format``

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 43
Project: Requester   Author: kylebebak   File: _format.py    MIT License 5 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to

        Returns:

            bool: Whether it conformed

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 44
Project: Requester   Author: kylebebak   File: _validators.py    MIT License 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 45
Project: TA-securitytrails   Author: secops4thewin   File: _validators.py    MIT License 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 46
Project: masakari   Author: openstack   File: test_api_validation.py    Apache License 2.0 5 votes vote down vote up
def test_format_checker_failed(self):
        format_checker = validators.FormatChecker()
        exc = self.assertRaises(jsonschema_exc.FormatError,
                                format_checker.check, "   ", "name")
        self.assertIsInstance(exc.cause, exception.InvalidName)
        self.assertEqual("An invalid 'name' value was provided. The name must "
                         "be: printable characters. "
                         "Can not start or end with whitespace.",
                         exc.cause.format_message()) 
Example 47
Project: masakari   Author: openstack   File: test_api_validation.py    Apache License 2.0 5 votes vote down vote up
def test_format_checker_failed_with_non_string(self):
        checks = ["name"]
        format_checker = validators.FormatChecker()

        for check in checks:
            exc = self.assertRaises(jsonschema_exc.FormatError,
                                    format_checker.check, None, "name")
            self.assertIsInstance(exc.cause, exception.InvalidName)
            self.assertEqual("An invalid 'name' value was provided. The name "
                             "must be: printable characters. "
                             "Can not start or end with whitespace.",
                             exc.cause.format_message()) 
Example 48
Project: Pizza-or-Not-a-Pizza   Author: mraza007   File: _validators.py    MIT License 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 49
Project: TA-bigfix   Author: jimmyatSplunk   File: _validators.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 50
Project: StupidSimplePatchServer   Author: brysontyrrell   File: _format.py    MIT License 5 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to


        Raises:

            :exc:`FormatError` if instance does not conform to ``format``

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 51
Project: StupidSimplePatchServer   Author: brysontyrrell   File: _format.py    MIT License 5 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to

        Returns:

            bool: Whether it conformed

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 52
Project: StupidSimplePatchServer   Author: brysontyrrell   File: _validators.py    MIT License 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 53
Project: StupidSimplePatchServer   Author: brysontyrrell   File: _format.py    MIT License 5 votes vote down vote up
def check(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to


        Raises:

            :exc:`FormatError` if instance does not conform to ``format``

        """

        if format not in self.checkers:
            return

        func, raises = self.checkers[format]
        result, cause = None, None
        try:
            result = func(instance)
        except raises as e:
            cause = e
        if not result:
            raise FormatError(
                "%r is not a %r" % (instance, format), cause=cause,
            ) 
Example 54
Project: StupidSimplePatchServer   Author: brysontyrrell   File: _format.py    MIT License 5 votes vote down vote up
def conforms(self, instance, format):
        """
        Check whether the instance conforms to the given format.

        Arguments:

            instance (any primitive type, i.e. str, number, bool):

                The instance to check

            format (str):

                The format that instance should conform to

        Returns:

            bool: Whether it conformed

        """

        try:
            self.check(instance, format)
        except FormatError:
            return False
        else:
            return True 
Example 55
Project: SA-ctf_scoreboard   Author: splunk   File: _validators.py    Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def format(validator, format, instance, schema):
    if validator.format_checker is not None:
        try:
            validator.format_checker.check(instance, format)
        except FormatError as error:
            yield ValidationError(error.message, cause=error.cause) 
Example 56
Project: shipyard   Author: att-comdev   File: json_schemas.py    Apache License 2.0 5 votes vote down vote up
def validate_json(json_string, schema):
    """
    invokes the validate function of jsonschema
    """
    schema_dict = json.loads(schema)
    schema_title = schema_dict['title']
    try:
        validate(json_string, schema_dict)
    except ValidationError as err:
        title = 'JSON validation failed: {}'.format(err.message)
        description = 'Failed validator: {} : {}'.format(
            err.validator,
            err.validator_value
        )
        LOG.error(title)
        LOG.error(description)
        raise InvalidFormatError(
            title=title,
            description=description,
        )
    except SchemaError as err:
        title = 'SchemaError: Unable to validate JSON: {}'.format(err)
        description = 'Invalid Schema: {}'.format(schema_title)
        LOG.error(title)
        LOG.error(description)
        raise AppError(
            title=title,
            description=description
        )
    except FormatError as err:
        title = 'FormatError: Unable to validate JSON: {}'.format(err)
        description = 'Invalid Format: {}'.format(schema_title)
        LOG.error(title)
        LOG.error(description)
        raise AppError(
            title=title,
            description=description
        )


# The action resource structure