Skip to content

Commit 24c1c68

Browse files
joeybloggsjoeybloggs
authored andcommitted
Add nostructlevel tag
* nostructlevel - Same as structonly tag except that any struct level validations will not run.
1 parent 27557e4 commit 24c1c68

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ Here is a list of the current built in validators:
138138
you know the struct will be valid, but need to verify it has been assigned.
139139
NOTE: only "required" and "omitempty" can be used on a struct itself.
140140
141+
nostructlevel
142+
Same as structonly tag except that any struct level validations will not run.
143+
141144
exists
142145
Is a special tag without a validation function attached. It is used when a field
143146
is a Pointer, Interface or Invalid and you wish to validate that it exists.

util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
skipValidationTag: emptyStructPtr,
2727
utf8HexComma: emptyStructPtr,
2828
utf8Pipe: emptyStructPtr,
29+
noStructLevelTag: emptyStructPtr,
2930
}
3031
)
3132

validator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
orSeparator = "|"
2727
tagKeySeparator = "="
2828
structOnlyTag = "structonly"
29+
noStructLevelTag = "nostructlevel"
2930
omitempty = "omitempty"
3031
skipValidationTag = "-"
3132
diveTag = "dive"
@@ -595,6 +596,11 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.
595596
typ = current.Type()
596597

597598
if typ != timeType {
599+
600+
if strings.Contains(tag, noStructLevelTag) {
601+
return
602+
}
603+
598604
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude, strings.Contains(tag, structOnlyTag))
599605
return
600606
}

validator_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,6 +3493,36 @@ func TestBase64Validation(t *testing.T) {
34933493
AssertError(t, errs, "", "", "base64")
34943494
}
34953495

3496+
func TestNoStructLevelValidation(t *testing.T) {
3497+
3498+
type Inner struct {
3499+
Test string `validate:"len=5"`
3500+
}
3501+
3502+
type Outer struct {
3503+
InnerStruct *Inner `validate:"required,nostructlevel"`
3504+
}
3505+
3506+
outer := &Outer{
3507+
InnerStruct: nil,
3508+
}
3509+
3510+
errs := validate.Struct(outer)
3511+
NotEqual(t, errs, nil)
3512+
AssertError(t, errs, "Outer.InnerStruct", "InnerStruct", "required")
3513+
3514+
inner := &Inner{
3515+
Test: "1234",
3516+
}
3517+
3518+
outer = &Outer{
3519+
InnerStruct: inner,
3520+
}
3521+
3522+
errs = validate.Struct(outer)
3523+
Equal(t, errs, nil)
3524+
}
3525+
34963526
func TestStructOnlyValidation(t *testing.T) {
34973527

34983528
type Inner struct {
@@ -3509,6 +3539,7 @@ func TestStructOnlyValidation(t *testing.T) {
35093539

35103540
errs := validate.Struct(outer)
35113541
NotEqual(t, errs, nil)
3542+
AssertError(t, errs, "Outer.InnerStruct", "InnerStruct", "required")
35123543

35133544
inner := &Inner{
35143545
Test: "1234",

0 commit comments

Comments
 (0)