SchemaValidator.validate()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
"""Schema implementation based on JSONSchema v4 draft
2
"""
3
4
from __future__ import (
5
    absolute_import,
6
    division,
7
    print_function
8
)
9
10
import logging
11
12
import jsonschema
0 ignored issues
show
Configuration introduced by
The import jsonschema could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
13
from jsonschema import (
0 ignored issues
show
Configuration introduced by
The import jsonschema could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
14
    SchemaError,
15
    ValidationError
16
)
17
18
_LOGGER = logging.getLogger(__name__)
19
20
21
class SchemaValidator(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
22
23
    __slots__ = ('_input_validator')
24
25
    def __init__(self, input_spec):
26
        if input_spec:
27
            try:
28
                self._input_validator = jsonschema.Draft4Validator(input_spec)
29
30
            except jsonschema.SchemaError:
31
                raise
32
33
        else:
34
            self._input_validator = None
35
36
    def validate(self, some_input):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
37
        if self._input_validator is not None:
38
            try:
39
                self._input_validator.validate(some_input)
40
                return True
41
42
            except jsonschema.ValidationError:
43
                raise
44
45
        else:
46
            # No input schema meams we accept everything
47
            return True
48
49
50
__all__ = [
51
    'SchemaError',
52
    'SchemaValidator',
53
    'ValidationError',
54
]
55