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

fix: preserve eager information when validating models (#664) #665

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
6 changes: 6 additions & 0 deletions executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ 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 := NewModel(model, c.Context())

isEager := c.eager
hasEagerFields := c.eagerFields

if err := sm.beforeValidate(c); err != nil {
return nil, err
}
Expand Down Expand Up @@ -155,6 +159,8 @@ func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...stri
}
}

c.eager = isEager
c.eagerFields = hasEagerFields
return verrs, c.Create(model, excludeColumns...)
}

Expand Down
4 changes: 4 additions & 0 deletions pop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ type Taxis []Taxi
// Validate gets run every time you call a "Validate*" (ValidateAndSave, ValidateAndCreate, ValidateAndUpdate) method.
// This method is not required and may be deleted.
func (b *Book) Validate(tx *Connection) (*validate.Errors, error) {
// Execute another query to test if Validate causes eager creation to fail
if err := tx.All(&Taxis{}); err != nil {
return nil, err
}
return validate.Validate(
&validators.StringIsPresent{Field: b.Description, Name: "Description"},
), nil
Expand Down