Skip to content

Commit 0fbc322

Browse files
joeybloggsjoeybloggs
authored andcommitted
rework code to allow handling of comma (,) and = within the params i.e. excludesall=,=
add test cases for comma and = validation within params add documentation stating how to include a comma within the parameters for #67
1 parent c6a510f commit 0fbc322

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ NOTE: Baked In Cross field validation only compares fields on the same struct,
143143
if cross field + cross struct validation is needed your own custom validator
144144
should be implemented.
145145
146+
NOTE2: comma is the default separator of validation tags, if you wish to have a comma
147+
included within the parameter i.e. excludesall=, you will need to use the UTF-8 hex
148+
representation 0x2C, which is replaced in the code as a comma, so the above will
149+
become excludesall=0x2C
150+
146151
Here is a list of the current built in validators:
147152
148153
-

validator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
)
2121

2222
const (
23+
utf8HexComma = "0x2C"
2324
tagSeparator = ","
2425
orSeparator = "|"
2526
noValidationTag = "-"
@@ -428,7 +429,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
428429
cField.tags = append(cField.tags, cTag)
429430

430431
for i, val := range orVals {
431-
vals := strings.Split(val, tagKeySeparator)
432+
vals := strings.SplitN(val, tagKeySeparator, 2)
432433

433434
key := strings.TrimSpace(vals[0])
434435

@@ -438,7 +439,7 @@ func (v *Validate) fieldWithNameAndValue(val interface{}, current interface{}, f
438439

439440
param := ""
440441
if len(vals) > 1 {
441-
param = vals[1]
442+
param = strings.Replace(vals[1], utf8HexComma, ",", -1)
442443
}
443444

444445
cTag.keyVals[i] = []string{key, param}

validator_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ func TestExcludesAllValidation(t *testing.T) {
281281

282282
err := validate.Field(username, "excludesall=@ ")
283283
NotEqual(t, err, nil)
284+
285+
excluded := ","
286+
287+
err = validate.Field(excluded, "excludesall=!@#$%^&*()_+.0x2C?")
288+
NotEqual(t, err, nil)
289+
290+
excluded = "="
291+
292+
err = validate.Field(excluded, "excludesall=!@#$%^&*()_+.0x2C=?")
293+
NotEqual(t, err, nil)
284294
}
285295

286296
func TestExcludesValidation(t *testing.T) {

0 commit comments

Comments
 (0)