Skip to content
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
7 changes: 7 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ Here is a list of the current built in validators:
colors to be accepted. This can also be combined with 'and' for example
( Usage: omitempty,rgb|rgba)

structonly
When a field that is a nest struct in encountered and contains this flag
any validation on the nested struct such as "required" will be run, but
none of the nested struct fields will be validated. This is usefull if
inside of you program you know the struct will be valid, but need to
verify it has been assigned.

omitempty
Allows conitional validation, for example if a field is not set with
a value (Determined by the required validator) then other validation
Expand Down
5 changes: 5 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
orSeparator = "|"
noValidationTag = "-"
tagKeySeparator = "="
structOnlyTag = "structonly"
omitempty = "omitempty"
validationFieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n"
validationStructErrMsg = "Struct:%s\n"
Expand Down Expand Up @@ -216,6 +217,10 @@ func (v *Validator) validateStructRecursive(top interface{}, s interface{}) *Str

} else {

if strings.Contains(tag, structOnlyTag) {
continue
}

if structErrors := v.validateStructRecursive(top, valueField.Interface()); structErrors != nil {
validationErrors.StructErrors[typeField.Name] = structErrors
// free up memory map no longer needed
Expand Down
29 changes: 29 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,35 @@ func isEqualFunc(val interface{}, field interface{}, param string) bool {
return val.(string) == field.(string)
}

func (ms *MySuite) TestStructOnlyValidation(c *C) {

type Inner struct {
Test string `validate:"len=5"`
}

type Outer struct {
InnerStruct *Inner `validate:"required,structonly"`
}

outer := &Outer{
InnerStruct: nil,
}

errs := myValidator.ValidateStruct(outer).Flatten()
c.Assert(errs, NotNil)

inner := &Inner{
Test: "1234",
}

outer = &Outer{
InnerStruct: inner,
}

errs = myValidator.ValidateStruct(outer).Flatten()
c.Assert(errs, IsNil)
}

func (ms *MySuite) TestGtField(c *C) {

type TimeTest struct {
Expand Down