Skip to content

Commit cddc415

Browse files
Dean KarnDean Karn
authored andcommitted
perf enhancements + corrected issue with or's
- or's weren't reporting the param back in the tag, long standing but noticed issue. - added validation check for or's when more validations exist after the or.
1 parent 42a0d6d commit cddc415

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package validator
22
================
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">
44
[![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5-
![Project status](https://img.shields.io/badge/version-9.1.0-green.svg)
5+
![Project status](https://img.shields.io/badge/version-9.1.1-green.svg)
66
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
77
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
88
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)

translations/en/en.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
12471247
translation: "{0} must contain a valid MAC address",
12481248
override: false,
12491249
},
1250+
{
1251+
tag: "iscolor",
1252+
translation: "{0} must be a valid color",
1253+
override: false,
1254+
},
12501255
}
12511256

12521257
for _, t := range translations {

translations/en/en_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func TestTranslations(t *testing.T) {
128128
IPAddrv6 string `validate:"ip6_addr"`
129129
UinxAddr string `validate:"unix_addr"` // can't fail from within Go's net package currently, but maybe in the future
130130
MAC string `validate:"mac"`
131+
IsColor string `validate:"iscolor"`
131132
}
132133

133134
var test Test
@@ -180,6 +181,10 @@ func TestTranslations(t *testing.T) {
180181
ns string
181182
expected string
182183
}{
184+
{
185+
ns: "Test.IsColor",
186+
expected: "IsColor must be a valid color",
187+
},
183188
{
184189
ns: "Test.MAC",
185190
expected: "MAC must contain a valid MAC address",

validator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ OUTER:
310310
v.misc = append(v.misc, '|')
311311
v.misc = append(v.misc, ct.tag...)
312312

313+
if len(ct.param) > 0 {
314+
v.misc = append(v.misc, '=')
315+
v.misc = append(v.misc, ct.param...)
316+
}
317+
313318
if ct.next == nil || ct.next.typeof != typeOr { // ct.typeof != typeOr
314319
// if we get here, no valid 'or' value and no more tags
315320

@@ -373,6 +378,10 @@ OUTER:
373378
v.flField = current
374379
v.flParam = ct.param
375380

381+
// // report error interface functions need these
382+
// v.ns = ns
383+
// v.actualNs = structNs
384+
376385
if !ct.fn(v) {
377386

378387
v.str1 = string(append(ns, cf.altName...))

validator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5504,7 +5504,7 @@ func TestOrTag(t *testing.T) {
55045504
s = "this ain't right"
55055505
errs = validate.Var(s, "rgb|rgba|len=10")
55065506
NotEqual(t, errs, nil)
5507-
AssertError(t, errs, "", "", "", "", "rgb|rgba|len")
5507+
AssertError(t, errs, "", "", "", "", "rgb|rgba|len=10")
55085508

55095509
s = "this is right"
55105510
errs = validate.Var(s, "rgb|rgba|len=13")

0 commit comments

Comments
 (0)