Skip to content

Commit f9b1333

Browse files
zeewellZeewell.Yu
authored and
Zeewell.Yu
committed
feat: Add support for omitting empty and zero values in validation
1 parent 6c3307e commit f9b1333

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

baked_in.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var (
5050
keysTag: {},
5151
endKeysTag: {},
5252
structOnlyTag: {},
53+
omitzero: {},
5354
omitempty: {},
5455
omitnil: {},
5556
skipValidationTag: {},
@@ -1796,6 +1797,20 @@ func hasValue(fl FieldLevel) bool {
17961797
}
17971798
}
17981799

1800+
// hasNotZeroValue is the validation function for validating if the current field's value is not the zero value for its type.
1801+
func hasNotZeroValue(fl FieldLevel) bool {
1802+
field := fl.Field()
1803+
switch field.Kind() {
1804+
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
1805+
return !field.IsNil()
1806+
default:
1807+
if fl.(*validate).fldIsPointer && field.Interface() != nil {
1808+
return !field.IsZero()
1809+
}
1810+
return field.IsValid() && !field.IsZero()
1811+
}
1812+
}
1813+
17991814
// requireCheckFieldKind is a func for check field kind
18001815
func requireCheckFieldKind(fl FieldLevel, param string, defaultNotFoundValue bool) bool {
18011816
field := fl.Field()

cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
typeKeys
2222
typeEndKeys
2323
typeOmitNil
24+
typeOmitZero
2425
)
2526

2627
const (
@@ -249,6 +250,10 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s
249250
}
250251
return
251252

253+
case omitzero:
254+
current.typeof = typeOmitZero
255+
continue
256+
252257
case omitempty:
253258
current.typeof = typeOmitEmpty
254259
continue

validator.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
117117
return
118118
}
119119

120+
if ct.typeof == typeOmitZero {
121+
return
122+
}
123+
120124
if ct.hasTag {
121125
if kind == reflect.Invalid {
122126
v.str1 = string(append(ns, cf.altName...))
@@ -238,6 +242,19 @@ OUTER:
238242
ct = ct.next
239243
continue
240244

245+
case typeOmitZero:
246+
v.slflParent = parent
247+
v.flField = current
248+
v.cf = cf
249+
v.ct = ct
250+
251+
if !hasNotZeroValue(v) {
252+
return
253+
}
254+
255+
ct = ct.next
256+
continue
257+
241258
case typeOmitNil:
242259
v.slflParent = parent
243260
v.flField = current

validator_instance.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
tagKeySeparator = "="
2222
structOnlyTag = "structonly"
2323
noStructLevelTag = "nostructlevel"
24+
omitzero = "omitzero"
2425
omitempty = "omitempty"
2526
omitnil = "omitnil"
2627
isdefault = "isdefault"

0 commit comments

Comments
 (0)