jsonschema: better errors unmarshaling ints

Distinguish among non-numbers, non-integral floating-point values,
and unrepresentable integers.

Change-Id: I0d60b92d6b4dd0baa536476931eb75e42ed5366a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/669695
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Jonathan Amsterdam 2025-05-02 17:25:43 -04:00
commit cc7bdd8d43

View file

@ -6,6 +6,8 @@ package jsonschema
import (
"encoding/json"
"fmt"
"math"
"regexp"
"testing"
)
@ -86,14 +88,17 @@ func TestUnmarshalErrors(t *testing.T) {
}{
{`1`, "cannot unmarshal number"},
{`{"type":1}`, `invalid value for "type"`},
{`{"minLength":1.5}`, `minLength:.*not an int`},
{`{"maxLength":1.5}`, `maxLength:.*not an int`},
{`{"minItems":1.5}`, `minItems:.*not an int`},
{`{"maxItems":1.5}`, `maxItems:.*not an int`},
{`{"minProperties":1.5}`, `minProperties:.*not an int`},
{`{"maxProperties":1.5}`, `maxProperties:.*not an int`},
{`{"minContains":1.5}`, `minContains:.*not an int`},
{`{"maxContains":1.5}`, `maxContains:.*not an int`},
{`{"minLength":1.5}`, `not an integer value`},
{`{"maxLength":1.5}`, `not an integer value`},
{`{"minItems":1.5}`, `not an integer value`},
{`{"maxItems":1.5}`, `not an integer value`},
{`{"minProperties":1.5}`, `not an integer value`},
{`{"maxProperties":1.5}`, `not an integer value`},
{`{"minContains":1.5}`, `not an integer value`},
{`{"maxContains":1.5}`, `not an integer value`},
{fmt.Sprintf(`{"maxContains":%d}`, int64(math.MaxInt32+1)), `out of range`},
{`{"minLength":9e99}`, `cannot be unmarshaled`},
{`{"minLength":"1.5"}`, `not a number`},
} {
var s Schema
err := json.Unmarshal([]byte(tt.in), &s)