feat(12-01): add Laravel request validation to lagoon

- lagoon.ValidateRequest ports Laravel 9 request validation: wildcard
  expansion, implicit-rule stop, bail, sometimes/nullable/blank skipping,
  size messages split by type and character-counted string lengths
- ParseRules, In, CustomRule, UploadedFile and ErrorKeys for rule tables
- pl/en lagoon::validation catalogs ported verbatim from WinterCMS
- lagoon.Validate answers a numeric range failure with the bound that
  failed (min, max or numeric between) instead of always max
This commit is contained in:
Jakub Zych
2026-10-02 11:25:36 +02:00
parent cfe568a214
commit f9b7f2ea33
11 changed files with 2598 additions and 37 deletions

View File

@@ -229,3 +229,43 @@ func TestValidateUniqueRespectsDeletedAt(t *testing.T) {
t.Fatalf("soft-deleted slug should pass unique: %v", errs)
}
}
func TestValidateNumericRangeMessagePicksFailedBound(t *testing.T) {
cases := []struct {
rule string
val any
want string
}{
{"integer|min:0", -1, "The views must be at least 0."},
{"integer|max:10", 11, "The views may not be greater than 10."},
{"numeric|min:0|max:10", "-0.5", "The views must be at least 0."},
{"numeric|min:0|max:10", "10.5", "The views may not be greater than 10."},
{"integer|between:0,10", 11, "The views must be between 0 and 10."},
{"integer|between:0,10", -1, "The views must be between 0 and 10."},
{"integer|between:0,10|min:2", 1, "The views must be at least 2."},
}
for _, c := range cases {
errs, err := Validate(t.Context(), nil, nil, map[string]string{"views": c.rule}, map[string]any{"views": c.val}, nil)
if err != nil {
t.Fatal(err)
}
if got := errs["views"]; len(got) != 1 || got[0] != c.want {
t.Fatalf("%s with %v = %v, want %q", c.rule, c.val, got, c.want)
}
}
errs, err := Validate(t.Context(), nil, nil, map[string]string{"views": "integer|between:0,10"}, map[string]any{"views": 5}, nil)
if err != nil || errs != nil {
t.Fatalf("in range: %v %v", errs, err)
}
}
func TestValidateNumericRangeMessageTranslated(t *testing.T) {
tr := requestTranslator(t)
errs, err := Validate(inLocale("pl"), nil, nil, map[string]string{"year": "integer|between:1889,2100"}, map[string]any{"year": 1700}, tr)
if err != nil {
t.Fatal(err)
}
if got := errs["year"]; len(got) != 1 || got[0] != "year musi zawierać się w granicach 1889 - 2100." {
t.Fatalf("pl between = %v", got)
}
}