Skip to content

Commit ca83d24

Browse files
Viicosdavidhewitt
andauthored
Fix validation of negative floats when using multiple_of (#1077)
Co-authored-by: David Hewitt <[email protected]>
1 parent 2f0c8c2 commit ca83d24

File tree

3 files changed

+3
-1
lines changed

3 files changed

+3
-1
lines changed

src/validators/float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Validator for ConstrainedFloatValidator {
109109
}
110110
if let Some(multiple_of) = self.multiple_of {
111111
let rem = float % multiple_of;
112-
let threshold = float / 1e9;
112+
let threshold = float.abs() / 1e9;
113113
if rem.abs() > threshold && (rem - multiple_of).abs() > threshold {
114114
return Err(ValError::new(
115115
ErrorType::MultipleOf {

tests/validators/test_decimal.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def test_decimal_kwargs(py_and_json: PyAndJson, kwargs: Dict[str, Any], input_va
188188
(0.1, 1, None),
189189
(0.1, 1.0, None),
190190
(0.1, int(5e10), None),
191+
(2.0, -2.0, None),
191192
],
192193
ids=repr,
193194
)

tests/validators/test_float.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def test_float_kwargs(py_and_json: PyAndJson, kwargs: Dict[str, Any], input_valu
121121
(0.1, 1, None),
122122
(0.1, 1.0, None),
123123
(0.1, int(5e10), None),
124+
(2.0, -2.0, None),
124125
],
125126
ids=repr,
126127
)

0 commit comments

Comments
 (0)