Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
5 changes: 3 additions & 2 deletions fastjsonschema/draft04.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
60 changes: 60 additions & 0 deletions tests/test_decimal_literals.py
Original file line number Diff line number Diff line change
@@ -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
Loading