Skip to content

Commit

Permalink
Fix boolean schema in if-then-else
Browse files Browse the repository at this point in the history
  • Loading branch information
horejsek committed Jul 22, 2023
1 parent 97634bd commit 6a67dc4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions fastjsonschema/draft06.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
2 changes: 1 addition & 1 deletion fastjsonschema/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions tests/test_boolean_schema.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6a67dc4

Please sign in to comment.