diff --git a/CHANGELOG.txt b/CHANGELOG.txt index c8c2953..06af31f 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,5 +1,6 @@ === UNRELEASED +* Fixed numeric `enum` and `const` guards rejecting matching Decimal values and treating Decimal literals as equal to booleans * Fixed `$` anchors preceded by an even number of backslashes in `pattern` and `patternProperties` matching before a trailing newline, and `$` inside a character class crashing compilation * Fixed `$` in `patternProperties` matching before a trailing newline, which also made a `pattern` with the same regex accept a trailing newline * Fixed `$ref` resolution for subschemas inside `allOf`, `anyOf`, `oneOf` and `items` arrays: an `$id` (or draft 04 `id`) there was not found, and a relative `$ref` there ignored the base URI of its parent `$id` diff --git a/fastjsonschema/draft04.py b/fastjsonschema/draft04.py index ae6d387..5804e0f 100644 --- a/fastjsonschema/draft04.py +++ b/fastjsonschema/draft04.py @@ -159,9 +159,10 @@ def generate_enum(self): def _enum_value_matches(self, var, value): if isinstance(value, bool): return 'isinstance({var}, bool) and {var} is {val}'.format(var=var, val=repr(value)) - if isinstance(value, (int, float)) and not isinstance(value, bool): + if isinstance(value, (int, float, decimal.Decimal)) and not isinstance(value, bool): return ( - 'isinstance({var}, (int, float)) and not isinstance({var}, bool) and {var} == {val}' + 'isinstance({var}, (int, float, Decimal)) ' + 'and not isinstance({var}, bool) and {var} == {val}' ).format(var=var, val=repr(value)) if value is None: return '{var} is None'.format(var=var) diff --git a/tests/test_decimal_literals.py b/tests/test_decimal_literals.py new file mode 100644 index 0000000..eb33be2 --- /dev/null +++ b/tests/test_decimal_literals.py @@ -0,0 +1,60 @@ +import decimal + +import pytest + +from fastjsonschema import ( + JsonSchemaValueException, + JsonSchemaValuesException, + compile, + compile_to_code, +) + + +@pytest.mark.parametrize('draft, keyword', ( + ('04', 'enum'), ('06', 'enum'), ('07', 'enum'), ('2019-09', 'enum'), + ('06', 'const'), ('07', 'const'), ('2019-09', 'const'), +)) +@pytest.mark.parametrize('generated', (False, True)) +@pytest.mark.parametrize('detailed_exceptions', (False, True)) +@pytest.mark.parametrize('fast_fail', (False, True)) +@pytest.mark.parametrize('literal, value, valid', ( + (1, decimal.Decimal('1.0'), True), + (1.5, decimal.Decimal('1.5'), True), + ({'n': 1}, {'n': decimal.Decimal('1.0')}, True), + ([1.5], [decimal.Decimal('1.5')], True), + (decimal.Decimal('1.0'), 1, True), + (decimal.Decimal('1.5'), decimal.Decimal('1.5'), True), + (decimal.Decimal('1.0'), True, False), + (decimal.Decimal('0.0'), False, False), + ({'n': decimal.Decimal('1.0')}, {'n': True}, False), + ([decimal.Decimal('0.0')], [False], False), + (1, decimal.Decimal('1.1'), False), + (decimal.Decimal('1.0'), '1', False), + (True, decimal.Decimal('1.0'), False), + (1, 1.0, True), + (1, True, False), +)) +def test_decimal_numeric_literals(draft, keyword, generated, detailed_exceptions, + fast_fail, literal, value, valid): + schema_uri = ( + 'https://json-schema.org/draft/2019-09/schema' + if draft == '2019-09' + else 'http://json-schema.org/draft-{}/schema#'.format(draft) + ) + definition = {'$schema': schema_uri, keyword: [literal] if keyword == 'enum' else literal} + options = {'detailed_exceptions': detailed_exceptions, 'fast_fail': fast_fail} + if generated: + namespace = {} + exec(compile_to_code(definition, **options), namespace) + validator = namespace['validate'] + else: + validator = compile(definition, **options) + + if valid: + assert validator(value) is value + else: + error = JsonSchemaValueException if fast_fail else JsonSchemaValuesException + with pytest.raises(error) as exc: + validator(value) + if detailed_exceptions and fast_fail: + assert exc.value.rule == keyword