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

fix multipleOf validation fails for small multipleOf with a higher number to validate due to precision #401

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions pkg/generator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,31 @@ func (v *numericValidator) generate(out *codegen.Emitter) {
}

if v.multipleOf != nil {

if v.roundToInt {
out.Printlnf(`if %s %s%s %% %v != 0 {`, checkPointer, pointerPrefix, value, v.valueOf(*v.multipleOf))
out.Indent(1)
out.Printlnf(`return fmt.Errorf("field %%s: must be a multiple of %%v", "%s", %f)`, v.jsonName, *v.multipleOf)
out.Indent(-1)
out.Printlnf("}")
} else {
if v.isNillable {
out.Printlnf(`if %s != nil {`, value)
} else {
out.Printlnf("{")
}
out.Indent(1)
out.Printlnf("remainder := math.Mod(%s%s, %v)", pointerPrefix, value, v.valueOf(*v.multipleOf))
out.Printlnf(
`if %s math.Abs(math.Mod(%s%s, %v)) > 1e-10 {`, checkPointer, pointerPrefix, value, v.valueOf(*v.multipleOf))
}
`if !(math.Abs(remainder) < 1e-10 || math.Abs(remainder - %v) < 1e-10) {`, v.valueOf(*v.multipleOf))
out.Indent(1)
out.Printlnf(`return fmt.Errorf("field %%s: must be a multiple of %%v", "%s", %f)`, v.jsonName, *v.multipleOf)
out.Indent(-1)
out.Printlnf("}")

out.Indent(1)
out.Printlnf(`return fmt.Errorf("field %%s: must be a multiple of %%v", "%s", %f)`, v.jsonName, *v.multipleOf)
out.Indent(-1)
out.Printlnf("}")
out.Indent(-1)
out.Printlnf("}")
}
}

nMin, nMax, nMinExclusive, nMaxExclusive := mathutils.NormalizeBounds(
Expand Down
16 changes: 11 additions & 5 deletions tests/data/validation/multipleOf/multipleOf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/data/validation/multipleOf/multipleOf.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"properties": {
"myNumber": {
"type": "number",
"multipleOf": 1.2
"multipleOf": 0.2
},
"myInteger": {
"type": "integer",
"multipleOf": 2
},
"myNullableNumber": {
"type": "number",
"multipleOf": 1.2
"multipleOf": 0.2
},
"myNullableInteger": {
"type": "integer",
Expand All @@ -24,4 +24,4 @@
"myNumber",
"myInteger"
]
}
}
6 changes: 5 additions & 1 deletion tests/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func TestMultipleOf(t *testing.T) {
desc: "no violations",
data: `{"myInteger": 10, "myNumber": 2.4}`,
},
{
desc: "no violations bigger number",
data: `{"myInteger": 10, "myNumber": 482.6}`,
},
{
desc: "myInt not a multiple of 2",
data: `{"myInteger": 11, "myNumber": 2.4}`,
Expand All @@ -238,7 +242,7 @@ func TestMultipleOf(t *testing.T) {
{
desc: "myNumber not a multiple of 1.2",
data: `{"myInteger": 10, "myNumber": 2.5}`,
wantErr: errors.New("field myNumber: must be a multiple of 1.2"),
wantErr: errors.New("field myNumber: must be a multiple of 0.2"),
},
}

Expand Down
Loading