feat(05-02): add lagoon.Validate rule-string engine

- Translate Laravel rule strings onto validator.Var and unique:table
- Custom money range via big.Rat; unique checks skip soft-deleted rows
- Fill zeros nil values and Scans jsonable columns
This commit is contained in:
Jakub Zych
2026-09-18 19:22:04 +02:00
parent 20b0199579
commit 2c5d92d659
5 changed files with 490 additions and 12 deletions

View File

@@ -1,6 +1,8 @@
package lagoon
import (
"database/sql"
"encoding/json"
"fmt"
"log/slog"
"reflect"
@@ -95,8 +97,11 @@ func setField(field reflect.Value, val any) error {
return fmt.Errorf("field cannot be set")
}
if val == nil {
if field.Kind() == reflect.Ptr {
field.Set(reflect.Zero(field.Type()))
field.Set(reflect.Zero(field.Type()))
if field.CanAddr() {
if scanner, ok := field.Addr().Interface().(sql.Scanner); ok {
return scanner.Scan(nil)
}
}
return nil
}
@@ -113,11 +118,33 @@ func setField(field reflect.Value, val any) error {
return nil
}
converted, err := convertValue(src, field.Type())
if err != nil {
return err
if err == nil {
field.Set(converted)
return nil
}
if field.CanAddr() {
if scanner, ok := field.Addr().Interface().(sql.Scanner); ok {
scanSrc, scanErr := fillScanSource(val)
if scanErr != nil {
return scanErr
}
return scanner.Scan(scanSrc)
}
}
return err
}
func fillScanSource(val any) (any, error) {
switch v := val.(type) {
case string, []byte:
return v, nil
default:
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
return b, nil
}
field.Set(converted)
return nil
}
func convertValue(src reflect.Value, destType reflect.Type) (reflect.Value, error) {

320
lagoon/validate.go Normal file
View File

@@ -0,0 +1,320 @@
package lagoon
import (
"context"
"fmt"
"math/big"
"reflect"
"regexp"
"strings"
"git.golem15.com/golem15/summercms/phrasebook"
"github.com/go-playground/validator/v10"
"gorm.io/gorm"
)
var identName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
var validateOnce = validator.New(validator.WithRequiredStructEnabled())
// Validate translates Laravel-style rule strings onto validator.Var() plus a
// unique:table DB check. Unrecognized tokens fail loudly. The returned map is
// the Laravel-shaped errors object (field -> messages); the HTTP envelope is
// a later-phase concern.
func Validate(ctx context.Context, tx *gorm.DB, model any, rules map[string]string, values map[string]any, tr *phrasebook.Translator) (map[string][]string, error) {
out := map[string][]string{}
for field, rule := range rules {
val, _ := values[field]
msgs, err := validateField(ctx, tx, model, field, rule, val, tr)
if err != nil {
return nil, err
}
if len(msgs) > 0 {
out[field] = msgs
}
}
if len(out) == 0 {
return nil, nil
}
return out, nil
}
func validateField(ctx context.Context, tx *gorm.DB, model any, field, rule string, val any, tr *phrasebook.Translator) ([]string, error) {
tokens := splitRule(rule)
nullable := false
required := false
var tags []string
var uniqueTable string
var moneyMin, moneyMax string
hasNumeric := false
for _, tok := range tokens {
name, arg, _ := strings.Cut(tok, ":")
switch name {
case "nullable":
nullable = true
tags = append(tags, "omitempty")
case "required":
required = true
tags = append(tags, "required")
case "integer":
if !isIntegerValue(val) && !isEmptyValue(val) {
return []string{validateMessage(ctx, tr, "integer", field, nil)}, nil
}
case "numeric":
hasNumeric = true
tags = append(tags, "numeric")
case "between":
x, y, ok := strings.Cut(arg, ",")
if !ok {
return nil, fmt.Errorf("lagoon: unrecognized validation rule %q", tok)
}
tags = append(tags, "min="+x, "max="+y)
case "min":
if hasNumeric {
moneyMin = arg
} else {
tags = append(tags, "min="+arg)
}
case "max":
if hasNumeric {
moneyMax = arg
} else {
tags = append(tags, "max="+arg)
}
case "in":
tags = append(tags, oneofTag(strings.Split(arg, ",")))
case "unique":
uniqueTable = arg
default:
return nil, fmt.Errorf("lagoon: unrecognized validation rule %q", tok)
}
}
if nullable && isEmptyValue(val) && !required {
return nil, nil
}
if hasNumeric && (moneyMin != "" || moneyMax != "") {
s := strings.TrimSpace(fmt.Sprint(val))
r := new(big.Rat)
if _, ok := r.SetString(s); !ok {
return []string{validateMessage(ctx, tr, "numeric", field, nil)}, nil
}
if !moneyInRange(val, moneyMin, moneyMax) {
return []string{validateMessage(ctx, tr, "max", field, map[string]string{"max": moneyMax, "min": moneyMin})}, nil
}
tags = withoutTag(tags, "numeric")
}
if len(tags) > 0 {
tag := strings.Join(tags, ",")
if err := validateOnce.Var(val, tag); err != nil {
ruleName := "required"
if !required {
ruleName = firstNonOmit(tags)
}
return []string{validateMessage(ctx, tr, ruleName, field, nil)}, nil
}
}
if uniqueTable != "" {
ok, err := uniqueOK(tx, model, uniqueTable, field, val)
if err != nil {
return nil, err
}
if !ok {
return []string{validateMessage(ctx, tr, "unique", field, nil)}, nil
}
}
return nil, nil
}
func splitRule(rule string) []string {
var out []string
for _, p := range strings.Split(rule, "|") {
p = strings.TrimSpace(p)
if p != "" {
out = append(out, p)
}
}
return out
}
func oneofTag(vals []string) string {
parts := make([]string, 0, len(vals))
for _, v := range vals {
v = strings.TrimSpace(v)
if v == "" {
continue
}
if strings.ContainsAny(v, " \t\"'") {
parts = append(parts, "'"+v+"'")
} else {
parts = append(parts, v)
}
}
return "oneof=" + strings.Join(parts, " ")
}
func isEmptyValue(val any) bool {
if val == nil {
return true
}
rv := reflect.ValueOf(val)
switch rv.Kind() {
case reflect.Ptr, reflect.Interface:
return rv.IsNil()
case reflect.String, reflect.Slice, reflect.Map:
return rv.Len() == 0
default:
return false
}
}
func isIntegerValue(val any) bool {
switch v := val.(type) {
case nil:
return true
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
case string:
if v == "" {
return true
}
_, ok := new(big.Int).SetString(v, 10)
return ok
default:
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.Ptr && rv.IsNil() {
return true
}
return false
}
}
func moneyInRange(val any, min, max string) bool {
s := strings.TrimSpace(fmt.Sprint(val))
if s == "" || s == "<nil>" {
return true
}
r := new(big.Rat)
if _, ok := r.SetString(s); !ok {
return false
}
if min != "" {
m := new(big.Rat)
if _, ok := m.SetString(min); ok && r.Cmp(m) < 0 {
return false
}
}
if max != "" {
m := new(big.Rat)
if _, ok := m.SetString(max); ok && r.Cmp(m) > 0 {
return false
}
}
return true
}
func uniqueOK(tx *gorm.DB, model any, table, column string, val any) (bool, error) {
if tx == nil {
return false, fmt.Errorf("lagoon: unique:%s requires a database handle", table)
}
if !identName.MatchString(table) || !identName.MatchString(column) {
return false, fmt.Errorf("lagoon: unique identifier %q.%q is not safe", table, column)
}
if isEmptyValue(val) {
return true, nil
}
q := tx.Table(table).Where(column+" = ?", val)
if tx.Migrator().HasColumn(table, "deleted_at") {
q = q.Where("deleted_at IS NULL")
}
if id := modelUintID(model); id != 0 && tx.Migrator().HasColumn(table, "id") {
q = q.Where("id <> ?", id)
}
var n int64
if err := q.Count(&n).Error; err != nil {
return false, err
}
return n == 0, nil
}
func modelUintID(model any) uint {
if model == nil {
return 0
}
rv := reflect.ValueOf(model)
if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
return 0
}
rv = rv.Elem()
}
if rv.Kind() != reflect.Struct {
return 0
}
f := rv.FieldByName("ID")
if !f.IsValid() {
return 0
}
switch f.Kind() {
case reflect.Uint, reflect.Uint32, reflect.Uint64:
return uint(f.Uint())
case reflect.Int, reflect.Int32, reflect.Int64:
if f.Int() < 0 {
return 0
}
return uint(f.Int())
}
return 0
}
func withoutTag(tags []string, name string) []string {
out := tags[:0]
for _, t := range tags {
n, _, _ := strings.Cut(t, "=")
if n != name {
out = append(out, t)
}
}
return out
}
func firstNonOmit(tags []string) string {
for _, t := range tags {
name, _, _ := strings.Cut(t, "=")
if name != "omitempty" && name != "required" {
return name
}
}
return "invalid"
}
func validateMessage(ctx context.Context, tr *phrasebook.Translator, rule, field string, params map[string]string) string {
if params == nil {
params = map[string]string{}
}
params["attribute"] = field
key := "lagoon.validate." + rule
if tr != nil {
s := tr.Get(ctx, key, params)
if s != "" && s != key {
return s
}
}
switch rule {
case "required":
return "The " + field + " field is required."
case "integer":
return "The " + field + " must be an integer."
case "numeric":
return "The " + field + " must be a number."
case "unique":
return "The " + field + " has already been taken."
case "max":
return "The " + field + " may not be greater than " + params["max"] + "."
case "min":
return "The " + field + " must be at least " + params["min"] + "."
case "oneof":
return "The selected " + field + " is invalid."
default:
return "The " + field + " is invalid."
}
}

114
lagoon/validate_test.go Normal file
View File

@@ -0,0 +1,114 @@
package lagoon
import (
"strings"
"testing"
)
func TestValidateBetweenYear(t *testing.T) {
rules := map[string]string{"year": "nullable|integer|between:1889,2100"}
errs, err := Validate(t.Context(), nil, nil, rules, map[string]any{"year": 1700}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs["year"]) == 0 {
t.Fatal("year=1700 must fail")
}
errs, err = Validate(t.Context(), nil, nil, rules, map[string]any{"year": nil}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs) != 0 {
t.Fatalf("nil year = %v", errs)
}
errs, err = Validate(t.Context(), nil, nil, rules, map[string]any{"year": 1991}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs) != 0 {
t.Fatalf("1991 = %v", errs)
}
}
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)
if err != nil {
t.Fatal(err)
}
if len(errs) != 0 {
t.Fatalf("EP 7\" rejected: %v", errs)
}
errs, err = Validate(t.Context(), nil, nil, rules, map[string]any{"format": "tape"}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs["format"]) == 0 {
t.Fatal("invalid format must fail")
}
}
func TestValidateMoneyRange(t *testing.T) {
rules := map[string]string{"market_price_stored": "nullable|numeric|min:0|max:999999.9999"}
errs, err := Validate(t.Context(), nil, nil, rules, map[string]any{"market_price_stored": "1000000.0000"}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs["market_price_stored"]) == 0 {
t.Fatal("1000000.0000 must fail max")
}
errs, err = Validate(t.Context(), nil, nil, rules, map[string]any{"market_price_stored": "25.0000"}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs) != 0 {
t.Fatalf("25.0000 = %v", errs)
}
errs, err = Validate(t.Context(), nil, nil, rules, map[string]any{"market_price_stored": nil}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs) != 0 {
t.Fatalf("nil money = %v", errs)
}
}
func TestValidateUnrecognizedRule(t *testing.T) {
_, err := Validate(t.Context(), nil, nil, map[string]string{"name": "required|nope"}, map[string]any{"name": "x"}, nil)
if err == nil || !strings.Contains(err.Error(), "nope") {
t.Fatalf("want unrecognized nope, got %v", err)
}
}
func TestValidateUniqueRespectsDeletedAt(t *testing.T) {
sqlDB, _ := dedicatedDB(t, "lagoon_validate_unique")
gdb, err := Use(t.Context(), sqlDB)
if err != nil {
t.Fatal(err)
}
if err := gdb.Exec(`CREATE TABLE lagoon_unique_rows (
id SERIAL PRIMARY KEY,
slug TEXT NOT NULL,
deleted_at TIMESTAMPTZ
)`).Error; err != nil {
t.Fatal(err)
}
if err := gdb.Exec(`INSERT INTO lagoon_unique_rows (slug, deleted_at) VALUES ('live', NULL), ('gone', NOW())`).Error; err != nil {
t.Fatal(err)
}
rules := map[string]string{"slug": "unique:lagoon_unique_rows"}
errs, err := Validate(t.Context(), gdb, nil, rules, map[string]any{"slug": "live"}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs["slug"]) == 0 {
t.Fatal("live slug must fail unique")
}
errs, err = Validate(t.Context(), gdb, nil, rules, map[string]any{"slug": "gone"}, nil)
if err != nil {
t.Fatal(err)
}
if len(errs) != 0 {
t.Fatalf("soft-deleted slug should pass unique: %v", errs)
}
}