Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect implementation on 'Multiple of' #26

Open
zenkio opened this issue Jul 9, 2023 · 0 comments
Open

Incorrect implementation on 'Multiple of' #26

zenkio opened this issue Jul 9, 2023 · 0 comments

Comments

@zenkio
Copy link

zenkio commented Jul 9, 2023

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),
		}
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant