fix(05): CR-01 accept pointer and whole JSON numbers in integer rule

This commit is contained in:
Jakub Zych
2026-09-18 23:41:57 +02:00
parent ba8a822149
commit 3943ea3a2e
2 changed files with 61 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ package lagoon
import (
"context"
"fmt"
"math"
"math/big"
"reflect"
"regexp"
@@ -169,23 +170,36 @@ func isEmptyValue(val any) bool {
}
}
// isIntegerValue reports whether val is an integer in Laravel's sense. It
// dereferences pointers (model fields such as *int) and accepts whole
// floating-point numbers, the shape encoding/json gives a map[string]any.
func isIntegerValue(val any) bool {
switch v := val.(type) {
case nil:
if val == nil {
return true
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
case string:
if v == "" {
}
rv := reflect.ValueOf(val)
for rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Interface {
if rv.IsNil() {
return true
}
_, ok := new(big.Int).SetString(v, 10)
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
case reflect.Float32, reflect.Float64:
f := rv.Float()
return f == math.Trunc(f) && !math.IsInf(f, 0)
case reflect.String:
s := rv.String()
if s == "" {
return true
}
_, ok := new(big.Int).SetString(s, 10)
return ok
default:
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.Ptr && rv.IsNil() {
return true
}
return false
}
}