#1618·go-swagger

issue with validation of multipleOf with the factor being float

Author: mqiuCreated Jul 12, 2018Updated Jun 28, 2026
Labelsbugfuture/maybevalidator

Problem statement

When I set the multipleOf value with a float point number, the validation may not always work. For example, if I set the value as multipleOf 0.01, most of the time the number is totally fine, but occasionally it will returning with XXX in body should be a multiple of xxx It seems that when trying to call for MultipleOf method, it will multiple the 1/multipleOf value and adjust the source value. However, when it trying to eventually calling for swag.IsFloat64AJSONInteger, it will convert it into int or uint and compare to be sure. However, one of the issue is that the number passed in is still a float and it may not match when force convert into an int. Like f := 1 / 0.01 * 67.15000001; f == float64(int64(f)) will be evaluated as false

Maybe instead of using "==" to compare, the code can change to compare if the abs diff is smaller than a minimal number.

Swagger specification

  floor:
    type: "number"
    description: "default floor val of the unit"
    minimum: 0
    maximum: 100
    multipleOf: 0.01