Skip to content

Commit 7f071bd

Browse files
ericfitzclaude
andcommitted
fix(api): change Note GORM hook from BeforeSave to BeforeCreate
The Note model's BeforeSave hook validated name/content as non-empty, but the Update path uses map-based GORM Updates() on an empty model struct. GORM runs BeforeSave on that empty struct, not the update map, causing false "name: cannot be empty" errors on every note update. Moves required-field validation into the existing BeforeCreate hook in models.go. Update-time validation is already handled by the API layer. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ab20318 commit 7f071bd

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"major": 1,
33
"minor": 2,
4-
"patch": 0,
4+
"patch": 1,
55
"prerelease": ""
66
}

api/models/hooks.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,10 @@ func (d *Document) BeforeSave(tx *gorm.DB) error {
9292
}
9393

9494
// --- Note Hooks ---
95-
96-
// BeforeSave validates Note before create or update
97-
func (n *Note) BeforeSave(tx *gorm.DB) error {
98-
if err := validation.ValidateNonEmpty("name", n.Name); err != nil {
99-
return err
100-
}
101-
if err := validation.ValidateNonEmpty("content", string(n.Content)); err != nil {
102-
return err
103-
}
104-
return nil
105-
}
95+
// Note: Required field validation (name, content) is in models.go BeforeCreate,
96+
// not here, because the Update path uses map-based GORM Updates() on an empty
97+
// model struct. A BeforeSave hook would validate the empty struct's zero-value
98+
// fields, causing false "cannot be empty" errors.
10699

107100
// --- Repository Hooks ---
108101

api/models/models.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/ericfitz/tmi/api/validation"
1011
"github.com/google/uuid"
1112
"gorm.io/gorm"
1213
)
@@ -381,11 +382,21 @@ func (Note) TableName() string {
381382
return tableName("notes")
382383
}
383384

384-
// BeforeCreate generates a UUID if not set
385+
// BeforeCreate generates a UUID if not set and validates required fields.
386+
// Note: Required field validation is intentionally in BeforeCreate (not BeforeSave)
387+
// because the Update path uses map-based GORM Updates() on an empty model struct.
388+
// BeforeSave would validate the empty struct's zero-value fields, causing false
389+
// "cannot be empty" errors. Update-time validation is handled by the API layer.
385390
func (n *Note) BeforeCreate(tx *gorm.DB) error {
386391
if n.ID == "" {
387392
n.ID = uuid.New().String()
388393
}
394+
if err := validation.ValidateNonEmpty("name", n.Name); err != nil {
395+
return err
396+
}
397+
if err := validation.ValidateNonEmpty("content", string(n.Content)); err != nil {
398+
return err
399+
}
389400
return nil
390401
}
391402

api/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
// Minor version number
4747
VersionMinor = "2"
4848
// Patch version number
49-
VersionPatch = "0"
49+
VersionPatch = "1"
5050
// VersionPreRelease is the pre-release label (e.g., "rc.0", "beta.1"), empty for stable releases
5151
VersionPreRelease = ""
5252
// GitCommit is the git commit hash from build

0 commit comments

Comments
 (0)