Merge branch 'v3' into v4-development

pull/21/head
Dean Karn 10 years ago
commit 37ced95e7a
  1. 7
      doc.go
  2. 7
      validator.go
  3. 29
      validator_test.go

@ -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 colors to be accepted. This can also be combined with 'and' for example
( Usage: omitempty,rgb|rgba) ( 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 omitempty
Allows conitional validation, for example if a field is not set with Allows conitional validation, for example if a field is not set with
a value (Determined by the required validator) then other validation a value (Determined by the required validator) then other validation

@ -22,6 +22,7 @@ const (
orSeparator = "|" orSeparator = "|"
noValidationTag = "-" noValidationTag = "-"
tagKeySeparator = "=" tagKeySeparator = "="
structOnlyTag = "structonly"
omitempty = "omitempty" omitempty = "omitempty"
validationFieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n" validationFieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n"
validationStructErrMsg = "Struct:%s\n" validationStructErrMsg = "Struct:%s\n"
@ -216,7 +217,11 @@ func (v *Validator) validateStructRecursive(top interface{}, s interface{}) *Str
} else { } else {
if structErrors := v.ValidateStruct(valueField.Interface()); structErrors != nil { if strings.Contains(tag, structOnlyTag) {
continue
}
if structErrors := v.validateStructRecursive(top, valueField.Interface()); structErrors != nil {
validationErrors.StructErrors[typeField.Name] = structErrors validationErrors.StructErrors[typeField.Name] = structErrors
// free up memory map no longer needed // free up memory map no longer needed
structErrors = nil structErrors = nil

@ -137,6 +137,35 @@ func isEqualFunc(val interface{}, field interface{}, param string) bool {
return val.(string) == field.(string) 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) { func (ms *MySuite) TestGtField(c *C) {
type TimeTest struct { type TimeTest struct {

Loading…
Cancel
Save