Skip to content

Incorrect implementation on 'Multiple of' #26

@zenkio

Description

@zenkio

I have this in my schema:

"amount": {
                "type": "number",
                "multipleOf": 0.01
              },

The generated Validate function is

if math.Mod(*m.Amount, 0.01) != 0 {
		return &validationError{
			errType:  "multipleOf",
			path:     []interface{}{"Amount"},
			jsonPath: []interface{}{"amount"},
			message:  fmt.Sprintf("must be a multiple of 0.01 but was %v", *m.Amount),
		}
	}

So for the amount 2000, it reported the error as must be a multiple of 0.01 but was 2000

I understand the floating point characteristic makes an integer won't always return 0 when calculating math.Mod(integer, 0.01)

I have to modify it manually to correct the validation.

if math.Mod(*m.Amount*100, 1) != 0 {
		return &validationError{
			errType:  "multipleOf",
			path:     []interface{}{"Amount"},
			jsonPath: []interface{}{"amount"},
			message:  fmt.Sprintf("must be a multiple of 0.01 but was %v", *m.Amount),
		}
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions