Skip to content

Commit ab383b7

Browse files
Merge pull request #195 from lob/DXP-1402
fix: catch multiple of issue for decimals
2 parents 10347c4 + 63bd210 commit ab383b7

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

lob_python/model_utils.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -914,16 +914,22 @@ def check_validations(
914914
current_validations = validations[input_variable_path]
915915
if (is_json_validation_enabled('multipleOf', configuration) and
916916
'multiple_of' in current_validations and
917-
isinstance(input_values, (int, float)) and
918-
not (float(input_values) / current_validations['multiple_of']).is_integer()):
919-
# Note 'multipleOf' will be as good as the floating point arithmetic.
920-
raise ApiValueError(
921-
"Invalid value for `%s`, value must be a multiple of "
922-
"`%s`" % (
923-
input_variable_path[0],
924-
current_validations['multiple_of']
925-
)
926-
)
917+
isinstance(input_values, (int, float))):
918+
# since floating point arithmetic can be imprecise, we'll convert input_values to string
919+
# and determine whether the decimal place is in a value position (within 3 spots of the end of the string)
920+
try:
921+
decimal_index = str(input_values).index('.')
922+
if decimal_index < len(str(input_values)) - 3:
923+
raise ApiValueError(
924+
"Invalid value for `%s`, value must be a multiple of "
925+
"`%s`" % (
926+
input_variable_path[0],
927+
current_validations['multiple_of']
928+
)
929+
)
930+
except:
931+
# if no decimal, then it's a multiple of 0.01
932+
print("whole dollar amount")
927933

928934
if (is_json_validation_enabled('maxLength', configuration) and
929935
'max_length' in current_validations and

test/Integration/test_zip_lookups_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def test_lookup_error(self):
7474

7575
with self.assertRaises(Exception) as context:
7676
self.api.lookup(zip)
77-
self.assertTrue("invalid ZIP code" in context.exception.__str__())
77+
print(context.exception.__str__())
78+
self.assertTrue("invalid zip code" in context.exception.__str__())
7879

7980

8081
if __name__ == '__main__':

0 commit comments

Comments
 (0)