Python jsonschema.validators.Draft4Validator() Examples

The following are 13 code examples for showing how to use jsonschema.validators.Draft4Validator(). These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module jsonschema.validators , or try the search function .

Example 1
Project: misp42splunk   Author: remg427   File: test_format.py    License: 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 2
Project: misp42splunk   Author: remg427   File: test_format.py    License: 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    License: 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: deepWordBug   Author: QData   File: test_format.py    License: 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    License: 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: sample-data   Author: open-contracting   File: test_fictional_example.py    License: 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)

    for error in validator(schema, format_checker=FormatChecker()).iter_errors(data):
        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 7
Project: accelerated-data-lake   Author: aws-samples   File: test_format.py    License: 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 8
Project: Requester   Author: kylebebak   File: test_format.py    License: 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 9
Project: watcher   Author: openstack   File: test_compute.py    License: 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 10
Project: SA-ctf_scoreboard   Author: splunk   File: test_format.py    License: 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) 
Example 11
Project: openapi-spec-validator   Author: p1c2u   File: factories.py    License: Apache License 2.0 5 votes vote down vote up
def from_resolver(cls, spec_resolver):
        """Creates a customized Draft4ExtendedValidator.

        :param spec_resolver: resolver for the spec
        :type resolver: :class:`jsonschema.RefResolver`
        """
        spec_validators = cls._get_spec_validators(spec_resolver)
        return validators.extend(Draft4Validator, spec_validators) 
Example 12
Project: pyblish-qml   Author: pyblish   File: test_format.py    License: 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 13
Project: jsonmerge   Author: avian2   File: __init__.py    License: MIT License 4 votes vote down vote up
def __init__(self, schema, strategies=(), objclass_def='dict', objclass_menu=None,
            validatorclass=Draft4Validator):
        """Create a new Merger object.

        schema -- JSON schema to use when merging.
        strategies -- Any additional merge strategies to use during merge.
        objclass_def -- Name of the default class for JSON objects.
        objclass_menu -- Any additional classes for JSON objects.
        validatorclass -- JSON Schema validator class.

        strategies argument should be a dict mapping strategy names to
        instances of Strategy subclasses.

        objclass_def specifies the default class used for JSON objects when one
        is not specified in the schema. It should be 'dict' (dict built-in),
        'OrderedDict' (collections.OrderedDict) or one of the names specified
        in the objclass_menu argument. If not specified, 'dict' is used.

        objclass_menu argument should be a dictionary that maps a string name
        to a function or class that will return an empty dictionary-like object
        to use as a JSON object. The function must accept either no arguments
        or a dictionary-like object.

        validatorclass argument can be used to supply a validator class from
        jsonschema. This can be used for example to specify which JSON Schema
        draft version will be used during merge.
        """

        self.schema = schema

        if hasattr(validatorclass, 'ID_OF'):
            resolver = LocalRefResolver.from_schema(schema, id_of=validatorclass.ID_OF)
        else:
            # jsonschema<3.0.0
            resolver = LocalRefResolver.from_schema(schema)
        self.validator = validatorclass(schema, resolver=resolver)

        self.strategies = dict(self.STRATEGIES)
        self.strategies.update(strategies)

        self.objclass_menu = { 'dict': dict, 'OrderedDict': OrderedDict }
        if objclass_menu:
            self.objclass_menu.update(objclass_menu)

        self.objclass_menu['_default'] = self.objclass_menu[objclass_def]