Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Fix for validation issue with decimal numbers #2

Merged
merged 1 commit into from Feb 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions target_postgres/__init__.py
Expand Up @@ -13,13 +13,25 @@
from tempfile import NamedTemporaryFile

import pkg_resources
from jsonschema.validators import Draft4Validator
from jsonschema import Draft4Validator, FormatChecker
from decimal import Decimal
import singer
from target_postgres.db_sync import DbSync

logger = singer.get_logger()


def float_to_decimal(value):
'''Walk the given data structure and turn all instances of float into
double.'''
if isinstance(value, float):
return Decimal(str(value))
if isinstance(value, list):
return [float_to_decimal(child) for child in value]
if isinstance(value, dict):
return {k: float_to_decimal(v) for k, v in value.items()}
return value

def emit_state(state):
if state is not None:
line = json.dumps(state)
Expand Down Expand Up @@ -65,7 +77,7 @@ def persist_lines(config, lines):
stream = o['stream']

# Validate record
validators[stream].validate(o['record'])
validators[stream].validate(float_to_decimal(o['record']))

sync = stream_to_sync[stream]

Expand Down Expand Up @@ -93,7 +105,8 @@ def persist_lines(config, lines):
raise Exception("Line is missing required key 'stream': {}".format(line))
stream = o['stream']
schemas[stream] = o
validators[stream] = Draft4Validator(o['schema'])
schema = float_to_decimal(o['schema'])
validators[stream] = Draft4Validator(schema, format_checker=FormatChecker())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koszti why do we specifically need a FormatChecker now?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@micaelbergeron It doesn't look like it's actually necessary, since it concerns the format property and has nothing to do with decimals or multipleOf, but I don't think it hurts to keep it either.

Koszti may just have accidentally included two unrelated fixes at the same time when he submitted cubedevinc#11. This FormatChecker is still present in https://github.com/transferwise/pipelinewise-target-postgres/blob/master/target_postgres/__init__.py#L136, so we're probably OK keeping it here too.

I'm more than happy to submit a new PR that doesn't apply this change, though, if you prefer.

if 'key_properties' not in o:
raise Exception("key_properties field is required")
key_properties[stream] = o['key_properties']
Expand Down