Skip to content
This repository was archived by the owner on Nov 20, 2021. It is now read-only.

Commit f05d389

Browse files
Kyle TravisChrisRx
authored andcommitted
pkg/e2db: omit zero-value required fields from Update
Change Update behavior to no longer error when a zero value is passed for a field marked "required". Based on Insert validation, the field must already be set in the table, so Update will skip setting the field.
1 parent a546630 commit f05d389

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

pkg/e2db/tx.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,18 @@ func (tx *Tx) Update(iface interface{}) error {
155155
if reflect.DeepEqual(f.value.Interface(), dbFieldValue.Interface()) {
156156
continue
157157
}
158+
159+
// if field is required and zero-value, it is safe to presume it was
160+
// already set
161+
if f.hasTag("required") && f.isZero() {
162+
continue
163+
}
158164
for _, tag := range f.Tags {
159165
switch tag.Name {
160166
case "index":
161167
oldIdx := key.Index(m.Name, f.Name, toString(dbFieldValue.Interface()), id)
162168
newIdx := key.Index(m.Name, f.Name, toString(f.value.Interface()), id)
163169
indexes[oldIdx] = newIdx
164-
case "required":
165-
if f.isZero() {
166-
return errors.Wrap(ErrFieldRequired, f.Name)
167-
}
168170
case "unique":
169171
oldIdx := key.Unique(m.Name, f.Name, toString(dbFieldValue.Interface()))
170172
newIdx := key.Unique(m.Name, f.Name, toString(f.value.Interface()))

0 commit comments

Comments
 (0)