Skip to content

validator: check invalid floats NaN/Inf#249

Merged
santhosh-tekuri merged 1 commit intosanthosh-tekuri:boonfrom
vnykmshr:fix-nan-inf-panic
Feb 18, 2026
Merged

validator: check invalid floats NaN/Inf#249
santhosh-tekuri merged 1 commit intosanthosh-tekuri:boonfrom
vnykmshr:fix-nan-inf-panic

Conversation

@vnykmshr
Copy link
Copy Markdown
Contributor

Fixes #248.

typeOf() now returns invalidType for NaN/Inf float64 and float32 values, so they hit the existing InvalidJsonValue error path at validator.go:92-94 instead of reaching numValidate() where big.Rat.SetString returns nil.

What changed

  • schema.go: split the float64 and float32 cases out of the numeric type switch in typeOf(), adding math.IsNaN/math.IsInf guards that return invalidType
  • nan_inf_test.go: test that all three values (NaN, +Inf, -Inf) produce a validation error instead of a panic

Alternative considered

A guard at the top of numValidate() would also work, but catching it in typeOf() prevents these values from reaching any validation logic at all.

@vnykmshr vnykmshr changed the title fix: reject NaN/Inf float64 values in typeOf typeOf: reject NaN/Inf float64 values Feb 18, 2026
nan_inf_test.go Outdated
"github.com/santhosh-tekuri/jsonschema/v6"
)

func TestNaNInfPanic(t *testing.T) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change function name to TestInvalidFloats

nan_inf_test.go Outdated
if err != nil {
t.Fatal(err)
}
if err := c.AddResource("test.json", doc); err != nil {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use schema.json instead of test.json

@@ -0,0 +1,32 @@
package jsonschema_test
Copy link
Copy Markdown
Owner

@santhosh-tekuri santhosh-tekuri Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change filename to validator_test.go
change commit msg: validator: check invalid floats NaN/Inf

@vnykmshr vnykmshr changed the title typeOf: reject NaN/Inf float64 values validator: check invalid floats NaN/Inf Feb 18, 2026
@santhosh-tekuri santhosh-tekuri merged commit cfca0e3 into santhosh-tekuri:boon Feb 18, 2026
3 checks passed
@vnykmshr vnykmshr deleted the fix-nan-inf-panic branch February 25, 2026 16:02
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

Successfully merging this pull request may close these issues.

Validate panics on NaN/Inf float64 when schema has numeric constraints

2 participants