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

@@ -30,6 +30,42 @@ func TestValidateBetweenYear(t *testing.T) {
}
}
func TestValidateIntegerPointerAndJSONNumber(t *testing.T) {
rules := map[string]string{"year": "nullable|integer|between:1889,2100"}
year := 1991
var nilYear *int
pass := map[string]any{
"*int": &year,
"nil *int": nilYear,
"float64": float64(1991),
}
for name, val := range pass {
errs, err := Validate(t.Context(), nil, nil, rules, map[string]any{"year": val}, nil)
if err != nil {
t.Fatalf("%s: %v", name, err)
}
if len(errs) != 0 {
t.Fatalf("%s must pass, got %v", name, errs)
}
}
early := 1700
fail := map[string]any{
"fractional float": 1991.5,
"out of range *int": &early,
"bool": true,
"non-digit string": "19x1",
}
for name, val := range fail {
errs, err := Validate(t.Context(), nil, nil, rules, map[string]any{"year": val}, nil)
if err != nil {
t.Fatalf("%s: %v", name, err)
}
if len(errs["year"]) == 0 {
t.Fatalf("%s must fail", name)
}
}
}
func TestValidateOneofQuotedSpace(t *testing.T) {
rules := map[string]string{"format": `nullable|in:LP,2LP,CD,2CD,MC,Box,EP 7"`}
errs, err := Validate(t.Context(), nil, nil, rules, map[string]any{"format": `EP 7"`}, nil)