Skip to content

Commit 99e60fd

Browse files
smazurovclaude
andauthored
fix: use epsilon tolerance in multipleOf float validation (#989)
math.Mod produces tiny non-zero remainders for valid floats like 0.36 % 0.01 due to IEEE 754 precision, causing false negatives. Refs: #988 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c6694fa commit 99e60fd

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any,
493493
}
494494
}
495495
if s.MultipleOf != nil {
496-
if math.Mod(num, *s.MultipleOf) != 0 {
496+
if r := math.Mod(num, *s.MultipleOf); math.Abs(r) > 1e-9 && math.Abs(r-*s.MultipleOf) > 1e-9 {
497497
res.Add(path, v, s.msgMultipleOf)
498498
}
499499
}

validate_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ var validateTests = []struct {
212212
input: map[string]any{"value": 2},
213213
errs: []string{"expected number to be a multiple of 5"},
214214
},
215+
{
216+
name: "multiple of float success",
217+
typ: reflect.TypeFor[struct {
218+
Value float64 "json:\"value\" multipleOf:\"0.01\""
219+
}](),
220+
input: map[string]any{"value": 0.36},
221+
},
215222
{
216223
name: "string success",
217224
typ: reflect.TypeFor[string](),

0 commit comments

Comments
 (0)