diff --git a/fastjsonschema/draft06.py b/fastjsonschema/draft06.py index 517c4d5..f5aca06 100644 --- a/fastjsonschema/draft06.py +++ b/fastjsonschema/draft06.py @@ -40,6 +40,8 @@ def generate_boolean_schema(self): Means that schema can be specified by boolean. True means everything is valid, False everything is invalid. """ + if self._definition is True: + self.l('pass') if self._definition is False: self.exc('{name} must not be there') diff --git a/fastjsonschema/generator.py b/fastjsonschema/generator.py index f2d8ca0..a7f96c5 100644 --- a/fastjsonschema/generator.py +++ b/fastjsonschema/generator.py @@ -242,7 +242,7 @@ def l(self, line, *args, **kwds): name = name + '".format(**locals()) + "' context = dict( - self._definition or {}, + self._definition if self._definition and self._definition is not True else {}, variable=self._variable, name=name, **kwds diff --git a/tests/test_boolean_schema.py b/tests/test_boolean_schema.py new file mode 100644 index 0000000..9d4a228 --- /dev/null +++ b/tests/test_boolean_schema.py @@ -0,0 +1,73 @@ +import pytest + +from fastjsonschema import JsonSchemaValueException + + +BASE_SCHEMA = { + '$schema': 'http://json-schema.org/draft-07/schema', + "if": { + "const": 1 + }, +} + + +@pytest.mark.parametrize('value, expected', [ + (1, 1), + (2, 2), +]) +def test_boolean_schema_true_in_then(asserter, value, expected): + asserter({ + **BASE_SCHEMA, + 'then': True, + 'else': { + 'type': 'number' + }, + }, value, expected) + + +@pytest.mark.parametrize('value, expected', [ + (1, JsonSchemaValueException( + 'data must not be there', value=1, name='data', + definition=False, rule=None + )), + (2, 2), +]) +def test_boolean_schema_false_in_then(asserter, value, expected): + asserter({ + **BASE_SCHEMA, + 'then': False, + 'else': { + 'type': 'number' + }, + }, value, expected) + + +@pytest.mark.parametrize('value, expected', [ + (1, 1), + (2, 2), +]) +def test_boolean_schema_true_in_else(asserter, value, expected): + asserter({ + **BASE_SCHEMA, + 'then': { + 'type': 'number', + }, + 'else': True, + }, value, expected) + + +@pytest.mark.parametrize('value, expected', [ + (1, 1), + (2, JsonSchemaValueException( + 'data must not be there', value=2, name='data', + definition=False, rule=None + )), +]) +def test_boolean_schema_false_in_else(asserter, value, expected): + asserter({ + **BASE_SCHEMA, + 'then': { + 'type': 'number', + }, + 'else': False, + }, value, expected)