Python jsonschema.validators.Draft4Validator() Examples

The following are code examples for showing how to use jsonschema.validators.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: ocds-merge   Author: open-contracting   File: test_fixtures.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_valid(filename, schema):
    errors = 0

    with open(filename) as f:
        data = json.load(f)
    if filename.endswith('-versioned.json') or filename.endswith('-compiled.json'):
        data = [data]

    for datum in data:
        for error in validator(schema, format_checker=FormatChecker()).iter_errors(datum):
            errors += 1
            warnings.warn(json.dumps(error.instance, indent=2, separators=(',', ': ')))
            warnings.warn('{} ({})\n'.format(error.message, '/'.join(error.absolute_schema_path)))

    assert errors == 0, '{} is invalid. See warnings below.'.format(filename) 
Example 2
Project: misp42splunk   Author: remg427   File: test_format.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 3
Project: core   Author: getavalon   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 4
Project: git_for_splunk   Author: ChrisYounger   File: test_format.py    Apache License 2.0 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 5
Project: aiohttp_apiset   Author: aamalev   File: validate.py    Apache License 2.0 5 votes vote down vote up
def factory(cls, *args, **kwargs):
        factory = extend(Draft4Validator, dict(required=cls._required))
        return factory(*args, format_checker=cls.format_checker, **kwargs) 
Example 6
Project: falcon-confiante   Author: ecarrara   File: middleware.py    MIT License 5 votes vote down vote up
def get_validator_for_request_body(self, path, http_method):
        if (path, http_method) not in self.validators:
            request_body = self.openapi_spec["paths"][path][http_method].get(
                "requestBody"
            )
            validator = None
            if request_body:
                schema = request_body["content"][self.DEFAULT_CONTENT_TYPE]["schema"]
                validator = Draft4Validator(schema, format_checker=self.format_checker)
            self.validators[(path, http_method)] = validator

        return self.validators[(path, http_method)] 
Example 7
Project: falcon-confiante   Author: ecarrara   File: middleware.py    MIT License 5 votes vote down vote up
def get_validator_for_response(self, path, http_method, status_code):
        if (path, http_method, status_code) not in self.validators:
            response = self.openapi_spec["paths"][path][http_method]["responses"][
                status_code
            ]

            schema = response["content"][self.DEFAULT_CONTENT_TYPE]["schema"]
            validator = Draft4Validator(schema, format_checker=self.format_checker)

            self.validators[(path, http_method, status_code)] = validator

        return self.validators[(path, http_method, status_code)] 
Example 8
Project: pyblish-rpc   Author: pyblish   File: test_format.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 9
Project: pastebin2misp   Author: renzejongman   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format": "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 10
Project: TA-thehive   Author: remg427   File: test_format.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 11
Project: accelerated-data-lake   Author: aws-samples   File: test_format.py    Apache License 2.0 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format": "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 12
Project: TA-intezer   Author: secops4thewin   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 13
Project: Requester   Author: kylebebak   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format": "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 14
Project: ocdskit   Author: open-contracting   File: validate.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def handle(self):
        components = urlparse(self.args.schema)
        if components.scheme == 'file':
            with open(self.args.schema[7:]) as f:
                schema = json.load(f)
        else:
            schema = requests.get(self.args.schema).json()

        format_checker = FormatChecker()
        if self.args.check_urls:
            def check_url(instance):
                # See https://github.com/Julian/jsonschema/blob/master/jsonschema/_format.py
                if not isinstance(instance, str_types):
                    return True
                rfc3987.parse(instance, rule='URI')  # raises ValueError
                try:
                    response = requests.get(instance, timeout=self.args.timeout)
                    result = response.status_code in (200,)
                    if not result:
                        print('HTTP {} on GET {}'.format(response.status_code, instance))
                    return result
                except requests.exceptions.Timeout:
                    print('Timedout on GET {}'.format(instance))
                    return False

            format_checker.checks('uri', raises=(ValueError))(check_url)

        for i, data in enumerate(self.items()):
            errors = False
            for error in validator(schema, format_checker=format_checker).iter_errors(data):
                print('item {}: {} ({})'.format(i, error.message, '/'.join(error.absolute_schema_path)))
                errors = True
            if not errors and self.args.verbose:
                print('item {}: no errors'.format(i)) 
Example 15
Project: TA-securitytrails   Author: secops4thewin   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 16
Project: Pizza-or-Not-a-Pizza   Author: mraza007   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 17
Project: pyblish-starter   Author: pyblish   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 18
Project: TA-bigfix   Author: jimmyatSplunk   File: test_format.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 19
Project: watcher   Author: openstack   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def test_compute_schema(self):
        test_scope = fake_scopes.compute_scope
        validators.Draft4Validator(
            audit_template.AuditTemplatePostType._build_schema()
            ).validate(test_scope) 
Example 20
Project: StupidSimplePatchServer   Author: brysontyrrell   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format": "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 21
Project: StupidSimplePatchServer   Author: brysontyrrell   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format": "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 22
Project: SA-ctf_scoreboard   Author: splunk   File: test_format.py    Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause)