Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback before validate #450

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ func (m *Model) beforeDestroy(c *Connection) error {
return nil
}

// BeforeValidateable callback will be called before a record is
// validated during
// ValidateAndCreate, ValidateAndUpdate, or ValidateAndSave
type BeforeValidateable interface {
BeforeValidate(*Connection) error
}

func (m *Model) beforeValidate(c *Connection) error {
if x, ok := m.Value.(BeforeValidateable); ok {
return x.BeforeValidate(c)
}
return nil
}

// AfterDestroyable callback will be called after a record is
// destroyed in the database.
type AfterDestroyable interface {
Expand Down
5 changes: 5 additions & 0 deletions callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Test_Callbacks(t *testing.T) {
BeforeC: "BC",
BeforeU: "BU",
BeforeD: "BD",
BeforeV: "BV",
AfterS: "AS",
AfterC: "AC",
AfterU: "AU",
Expand Down Expand Up @@ -50,6 +51,10 @@ func Test_Callbacks(t *testing.T) {
r.Equal("BeforeDestroy", user.BeforeD)
r.Equal("AfterDestroy", user.AfterD)

verrs, err := tx.ValidateAndSave(user)
r.False(verrs.HasAny())
r.NoError(err)
r.Equal("BeforeValidate", user.BeforeV)
})
}

Expand Down
9 changes: 9 additions & 0 deletions executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func (q *Query) ExecWithCount() (int, error) {
// If model is a slice, each item of the slice is validated then saved in the database.
func (c *Connection) ValidateAndSave(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
if err := sm.beforeValidate(c); err != nil {
return nil, err
}
verrs, err := sm.validateSave(c)
if err != nil {
return verrs, err
Expand Down Expand Up @@ -93,6 +96,9 @@ func (c *Connection) Save(model interface{}, excludeColumns ...string) error {
// If model is a slice, each item of the slice is validated then created in the database.
func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
if err := sm.beforeValidate(c); err != nil {
return nil, err
}
verrs, err := sm.validateCreate(c)
if err != nil {
return verrs, err
Expand Down Expand Up @@ -313,6 +319,9 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
// If model is a slice, each item of the slice is validated then updated in the database.
func (c *Connection) ValidateAndUpdate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
if err := sm.beforeValidate(c); err != nil {
return nil, err
}
verrs, err := sm.validateUpdate(c)
if err != nil {
return verrs, err
Expand Down
1 change: 1 addition & 0 deletions migrations/20181104135800_callbacks_users.up.fizz
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ create_table("callbacks_users") {
t.Column("after_u", "string", {})
t.Column("after_d", "string", {})
t.Column("after_f", "string", {})
t.Column("before_v", "string", {})
t.Timestamps()
}
6 changes: 6 additions & 0 deletions pop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ type CallbacksUser struct {
BeforeC string `db:"before_c"`
BeforeU string `db:"before_u"`
BeforeD string `db:"before_d"`
BeforeV string `db:"before_v"`
AfterS string `db:"after_s"`
AfterC string `db:"after_c"`
AfterU string `db:"after_u"`
Expand Down Expand Up @@ -318,6 +319,11 @@ func (u *CallbacksUser) BeforeDestroy(tx *Connection) error {
return nil
}

func (u *CallbacksUser) BeforeValidate(tx *Connection) error {
u.BeforeV = "BeforeValidate"
return nil
}

func (u *CallbacksUser) AfterSave(tx *Connection) error {
u.AfterS = "AfterSave"
return nil
Expand Down