Merge branch 'v1-development' into v1

pull/16/head
Dean Karn 10 years ago
commit 87fff1c79c
  1. 20
      doc.go
  2. 73
      validator.go
  3. 28
      validator_test.go

@ -127,60 +127,80 @@ Here is a list of the current built in validators:
Tells the validation to skip this struct field; this is particularily Tells the validation to skip this struct field; this is particularily
handy in ignoring embedded structs from being validated. (Usage: -) handy in ignoring embedded structs from being validated. (Usage: -)
|
This is the 'or' operator allowing multiple validators to be used and
accepted. (Usage: rbg|rgba) <-- this would allow either rgb or rgba
colors to be accepted. This can also be combined with 'and' for example
( Usage: omitempty,rgb|rgba)
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
such as min or max won't run, but if a value is set validation will run. such as min or max won't run, but if a value is set validation will run.
(Usage: omitempty) (Usage: omitempty)
required required
This validates that the value is not the data types default value. This validates that the value is not the data types default value.
For numbers ensures value is not zero. For strings ensures value is For numbers ensures value is not zero. For strings ensures value is
not "". For slices, arrays, and maps, ensures the length is not zero. not "". For slices, arrays, and maps, ensures the length is not zero.
(Usage: required) (Usage: required)
len len
For numbers, max will ensure that the value is For numbers, max will ensure that the value is
equal to the parameter given. For strings, it checks that equal to the parameter given. For strings, it checks that
the string length is exactly that number of characters. For slices, the string length is exactly that number of characters. For slices,
arrays, and maps, validates the number of items. (Usage: len=10) arrays, and maps, validates the number of items. (Usage: len=10)
max max
For numbers, max will ensure that the value is For numbers, max will ensure that the value is
less than or equal to the parameter given. For strings, it checks less than or equal to the parameter given. For strings, it checks
that the string length is at most that number of characters. For that the string length is at most that number of characters. For
slices, arrays, and maps, validates the number of items. (Usage: max=10) slices, arrays, and maps, validates the number of items. (Usage: max=10)
min min
For numbers, min will ensure that the value is For numbers, min will ensure that the value is
greater or equal to the parameter given. For strings, it checks that greater or equal to the parameter given. For strings, it checks that
the string length is at least that number of characters. For slices, the string length is at least that number of characters. For slices,
arrays, and maps, validates the number of items. (Usage: min=10) arrays, and maps, validates the number of items. (Usage: min=10)
alpha alpha
This validates that a strings value contains alpha characters only This validates that a strings value contains alpha characters only
(Usage: alpha) (Usage: alpha)
alphanum alphanum
This validates that a strings value contains alphanumeric characters only This validates that a strings value contains alphanumeric characters only
(Usage: alphanum) (Usage: alphanum)
numeric numeric
This validates that a strings value contains a basic numeric value. This validates that a strings value contains a basic numeric value.
basic excludes exponents etc... basic excludes exponents etc...
(Usage: numeric) (Usage: numeric)
hexadecimal hexadecimal
This validates that a strings value contains a valid hexadecimal. This validates that a strings value contains a valid hexadecimal.
(Usage: hexadecimal) (Usage: hexadecimal)
hexcolor hexcolor
This validates that a strings value contains a valid hex color including This validates that a strings value contains a valid hex color including
hashtag (#) hashtag (#)
(Usage: hexcolor) (Usage: hexcolor)
rgb rgb
This validates that a strings value contains a valid rgb color This validates that a strings value contains a valid rgb color
(Usage: rgb) (Usage: rgb)
rgba rgba
This validates that a strings value contains a valid rgba color This validates that a strings value contains a valid rgba color
(Usage: rgba) (Usage: rgba)
hsl hsl
This validates that a strings value contains a valid hsl color This validates that a strings value contains a valid hsl color
(Usage: hsl) (Usage: hsl)
hsla hsla
This validates that a strings value contains a valid hsla color This validates that a strings value contains a valid hsla color
(Usage: hsla) (Usage: hsla)
email email
This validates that a strings value contains a valid email This validates that a strings value contains a valid email
This may not conform to all possibilities of any rfc standard, but neither This may not conform to all possibilities of any rfc standard, but neither

@ -288,7 +288,71 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st
for _, valTag := range valTags { for _, valTag := range valTags {
orVals := strings.Split(valTag, "|")
if len(orVals) > 1 {
errTag := ""
for _, val := range orVals {
key, err := v.validateFieldByNameAndSingleTag(f, name, val)
if err == nil {
return nil
}
errTag += "|" + key
}
errTag = strings.TrimLeft(errTag, "|")
return errors.New(errTag)
}
if _, err := v.validateFieldByNameAndSingleTag(f, name, valTag); err != nil {
return err
}
// TODO: validate = in regex's // TODO: validate = in regex's
// vals := strings.Split(valTag, "=")
// key := strings.Trim(vals[0], " ")
//
// if len(key) == 0 {
// panic(fmt.Sprintf("Invalid validation tag on field %s", name))
// }
//
// // OK to continue because we checked it's existance before getting into this loop
// if key == omitempty {
// continue
// }
//
// valFunc, ok := v.validationFuncs[key]
// if !ok {
// panic(fmt.Sprintf("Undefined validation function on field %s", name))
// }
//
// param := ""
// if len(vals) > 1 {
// param = strings.Trim(vals[1], " ")
// }
//
// if err := valFunc(f, param); !err {
//
// return errors.New(key)
// }
// if err := v.validateFieldByNameAndSingleTag(f, name, valTag); err != nil {
// return err
// }
}
return nil
}
func (v *Validator) validateFieldByNameAndSingleTag(f interface{}, name string, valTag string) (string, error) {
vals := strings.Split(valTag, "=") vals := strings.Split(valTag, "=")
key := strings.Trim(vals[0], " ") key := strings.Trim(vals[0], " ")
@ -298,7 +362,7 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st
// OK to continue because we checked it's existance before getting into this loop // OK to continue because we checked it's existance before getting into this loop
if key == omitempty { if key == omitempty {
continue return key, nil
} }
valFunc, ok := v.validationFuncs[key] valFunc, ok := v.validationFuncs[key]
@ -312,11 +376,8 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st
} }
if err := valFunc(f, param); !err { if err := valFunc(f, param); !err {
return key, errors.New(key)
return errors.New(key)
}
} }
return nil return key, nil
} }

@ -113,6 +113,34 @@ func AssertMapFieldError(s map[string]*validator.FieldValidationError, field str
c.Assert(val.ErrorTag, Equals, expectedTag) c.Assert(val.ErrorTag, Equals, expectedTag)
} }
func (ms *MySuite) TestOrTag(c *C) {
s := "rgba(0,31,255,0.5)"
err := validator.ValidateFieldByTag(s, "rgb|rgba")
c.Assert(err, IsNil)
s = "rgba(0,31,255,0.5)"
err = validator.ValidateFieldByTag(s, "rgb|rgba|len=18")
c.Assert(err, IsNil)
s = "this ain't right"
err = validator.ValidateFieldByTag(s, "rgb|rgba")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "rgb|rgba")
s = "this ain't right"
err = validator.ValidateFieldByTag(s, "rgb|rgba|len=10")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "rgb|rgba|len")
s = "this is right"
err = validator.ValidateFieldByTag(s, "rgb|rgba|len=13")
c.Assert(err, IsNil)
s = ""
err = validator.ValidateFieldByTag(s, "omitempty,rgb|rgba")
c.Assert(err, IsNil)
}
func (ms *MySuite) TestHsla(c *C) { func (ms *MySuite) TestHsla(c *C) {
s := "hsla(360,100%,100%,1)" s := "hsla(360,100%,100%,1)"

Loading…
Cancel
Save