Merge branch 'investigate-including-comma-in-data' into v5-development

pull/68/head
joeybloggs 10 years ago
commit 678d778cba
  1. 5
      doc.go
  2. 5
      validator.go
  3. 10
      validator_test.go

@ -143,6 +143,11 @@ NOTE: Baked In Cross field validation only compares fields on the same struct,
if cross field + cross struct validation is needed your own custom validator if cross field + cross struct validation is needed your own custom validator
should be implemented. should be implemented.
NOTE2: comma is the default separator of validation tags, if you wish to have a comma
included within the parameter i.e. excludesall=, you will need to use the UTF-8 hex
representation 0x2C, which is replaced in the code as a comma, so the above will
become excludesall=0x2C
Here is a list of the current built in validators: Here is a list of the current built in validators:
- -

@ -20,6 +20,7 @@ import (
) )
const ( const (
utf8HexComma = "0x2C"
tagSeparator = "," tagSeparator = ","
orSeparator = "|" orSeparator = "|"
noValidationTag = "-" noValidationTag = "-"
@ -428,7 +429,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
cField.tags = append(cField.tags, cTag) cField.tags = append(cField.tags, cTag)
for i, val := range orVals { for i, val := range orVals {
vals := strings.Split(val, tagKeySeparator) vals := strings.SplitN(val, tagKeySeparator, 2)
key := strings.TrimSpace(vals[0]) key := strings.TrimSpace(vals[0])
@ -438,7 +439,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
param := "" param := ""
if len(vals) > 1 { if len(vals) > 1 {
param = vals[1] param = strings.Replace(vals[1], utf8HexComma, ",", -1)
} }
cTag.keyVals[i] = []string{key, param} cTag.keyVals[i] = []string{key, param}

@ -281,6 +281,16 @@ func TestExcludesAllValidation(t *testing.T) {
err := validate.Field(username, "excludesall=@ ") err := validate.Field(username, "excludesall=@ ")
NotEqual(t, err, nil) NotEqual(t, err, nil)
excluded := ","
err = validate.Field(excluded, "excludesall=!@#$%^&*()_+.0x2C?")
NotEqual(t, err, nil)
excluded = "="
err = validate.Field(excluded, "excludesall=!@#$%^&*()_+.0x2C=?")
NotEqual(t, err, nil)
} }
func TestExcludesValidation(t *testing.T) { func TestExcludesValidation(t *testing.T) {

Loading…
Cancel
Save