Skip to content

Commit

Permalink
Merge pull request #195 from lob/DXP-1402
Browse files Browse the repository at this point in the history
fix: catch multiple of issue for decimals
  • Loading branch information
BennyKitchell authored Nov 21, 2022
2 parents 10347c4 + 63bd210 commit ab383b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
26 changes: 16 additions & 10 deletions lob_python/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,16 +914,22 @@ def check_validations(
current_validations = validations[input_variable_path]
if (is_json_validation_enabled('multipleOf', configuration) and
'multiple_of' in current_validations and
isinstance(input_values, (int, float)) and
not (float(input_values) / current_validations['multiple_of']).is_integer()):
# Note 'multipleOf' will be as good as the floating point arithmetic.
raise ApiValueError(
"Invalid value for `%s`, value must be a multiple of "
"`%s`" % (
input_variable_path[0],
current_validations['multiple_of']
)
)
isinstance(input_values, (int, float))):
# since floating point arithmetic can be imprecise, we'll convert input_values to string
# and determine whether the decimal place is in a value position (within 3 spots of the end of the string)
try:
decimal_index = str(input_values).index('.')
if decimal_index < len(str(input_values)) - 3:
raise ApiValueError(
"Invalid value for `%s`, value must be a multiple of "
"`%s`" % (
input_variable_path[0],
current_validations['multiple_of']
)
)
except:
# if no decimal, then it's a multiple of 0.01
print("whole dollar amount")

if (is_json_validation_enabled('maxLength', configuration) and
'max_length' in current_validations and
Expand Down
3 changes: 2 additions & 1 deletion test/Integration/test_zip_lookups_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def test_lookup_error(self):

with self.assertRaises(Exception) as context:
self.api.lookup(zip)
self.assertTrue("invalid ZIP code" in context.exception.__str__())
print(context.exception.__str__())
self.assertTrue("invalid zip code" in context.exception.__str__())


if __name__ == '__main__':
Expand Down

0 comments on commit ab383b7

Please sign in to comment.