Skip to content

Commit 04bdf37

Browse files
author
Frol Kruchkov
committed
int fixed
1 parent 0e1af88 commit 04bdf37

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

utils.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,13 @@ func isJsonNumber(what interface{}) bool {
106106
return false
107107
}
108108

109-
func checkJsonNumber(what interface{}) (isValidInt64 bool, isValidInt32 bool) {
109+
func checkJsonInteger(what interface{}) (isInt bool) {
110110

111111
jsonNumber := what.(json.Number)
112112

113113
bigFloat, isValidNumber := new(big.Float).SetString(string(jsonNumber))
114-
if !isValidNumber || !bigFloat.IsInt() {
115-
return false, false
116-
}
117-
118-
int64Value, acc := bigFloat.Int64()
119-
isValidInt64 = acc == big.Exact
120-
isValidInt32 = isValidInt64 && int64Value <= math.MaxInt32 && int64Value >= math.MinInt32
121114

122-
return
115+
return isValidNumber && bigFloat.IsInt()
123116

124117
}
125118

@@ -144,9 +137,9 @@ func mustBeInteger(what interface{}) *int {
144137

145138
number := what.(json.Number)
146139

147-
_, isValidInt32 := checkJsonNumber(number)
140+
isInt := checkJsonInteger(number)
148141

149-
if isValidInt32 {
142+
if isInt {
150143

151144
int64Value, err := number.Int64()
152145
if err != nil {

utils_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,26 @@ func TestResultErrorFormatNumber(t *testing.T) {
6666

6767
func TestCheckJsonNumber(t *testing.T) {
6868
var testCases = []struct {
69-
isInt64 bool
70-
isInt32 bool
71-
value json.Number
69+
isInt bool
70+
value json.Number
7271
}{
73-
{true, true, "0"},
74-
{true, true, "2147483647"},
75-
{true, true, "-2147483648"},
76-
{true, false, "9223372036854775807"},
77-
{true, false, "-9223372036854775808"},
78-
{true, true, "1.0e+2"},
79-
{true, false, "1.0e+10"},
80-
{true, true, "-1.0e+2"},
81-
{true, false, "-1.0e+10"},
82-
{false, false, "1.0e-2"},
72+
{true, "0"},
73+
{true, "2147483647"},
74+
{true, "-2147483648"},
75+
{true, "9223372036854775807"},
76+
{true, "-9223372036854775808"},
77+
{true, "1.0e+2"},
78+
{true, "1.0e+10"},
79+
{true, "-1.0e+2"},
80+
{true, "-1.0e+10"},
81+
{false, "1.0e-2"},
82+
{false, "number"},
83+
{false, "123number"},
8384
}
8485

8586
for _, testCase := range testCases {
86-
isInt64, isInt32 := checkJsonNumber(testCase.value)
87-
assert.Equal(t, testCase.isInt32, isInt32)
88-
assert.Equal(t, testCase.isInt64, isInt64)
87+
assert.Equal(t, testCase.isInt, checkJsonInteger(testCase.value))
88+
assert.Equal(t, testCase.isInt, checkJsonInteger(testCase.value))
8989
}
9090

9191
}

validation.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i
118118

119119
value := currentNode.(json.Number)
120120

121-
isValidInt64, _ := checkJsonNumber(value)
121+
isInt := checkJsonInteger(value)
122122

123-
validType := currentSubSchema.types.Contains(TYPE_NUMBER) || (isValidInt64 && currentSubSchema.types.Contains(TYPE_INTEGER))
123+
validType := currentSubSchema.types.Contains(TYPE_NUMBER) || (isInt && currentSubSchema.types.Contains(TYPE_INTEGER))
124124

125125
if currentSubSchema.types.IsTyped() && !validType {
126126

127127
givenType := TYPE_INTEGER
128-
if !isValidInt64 {
128+
if !isInt {
129129
givenType = TYPE_NUMBER
130130
}
131131

0 commit comments

Comments
 (0)