Skip to content

Commit 95609cd

Browse files
committed
Merge remote-tracking branch 'fixes/fix/multipleOf-validation-error'
2 parents 50876c3 + 65f2a3e commit 95609cd

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

target_postgres/__init__.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,24 @@
1313
from tempfile import TemporaryFile
1414

1515
import pkg_resources
16-
from jsonschema.validators import Draft4Validator
16+
from jsonschema.validators import Draft4Validator, FormatChecker
1717
import singer
1818
from target_postgres.db_sync import DbSync
1919

2020
logger = singer.get_logger()
2121

2222

23+
def float_to_decimal(value):
24+
'''Walk the given data structure and turn all instances of float into
25+
double.'''
26+
if isinstance(value, float):
27+
return Decimal(str(value))
28+
if isinstance(value, list):
29+
return [float_to_decimal(child) for child in value]
30+
if isinstance(value, dict):
31+
return {k: float_to_decimal(v) for k, v in value.items()}
32+
return value
33+
2334
def emit_state(state):
2435
if state is not None:
2536
line = json.dumps(state)
@@ -65,7 +76,7 @@ def persist_lines(config, lines):
6576
stream = o['stream']
6677

6778
# Validate record
68-
validators[stream].validate(o['record'])
79+
validators[stream].validate(float_to_decimal(o['record']))
6980

7081
sync = stream_to_sync[stream]
7182

@@ -93,7 +104,8 @@ def persist_lines(config, lines):
93104
raise Exception("Line is missing required key 'stream': {}".format(line))
94105
stream = o['stream']
95106
schemas[stream] = o
96-
validators[stream] = Draft4Validator(o['schema'])
107+
schema = float_to_decimal(o['schema'])
108+
validators[stream] = Draft4Validator(schema, format_checker=FormatChecker())
97109
if 'key_properties' not in o:
98110
raise Exception("key_properties field is required")
99111
key_properties[stream] = o['key_properties']

0 commit comments

Comments
 (0)