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.
pull/256/head
Dean Karn 8 years ago
parent 42a0d6df3b
commit cddc415625
  1. 2
      README.md
  2. 5
      translations/en/en.go
  3. 5
      translations/en/en_test.go
  4. 9
      validator.go
  5. 2
      validator_test.go

@ -2,7 +2,7 @@ Package validator
================ ================
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png"> <img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">
[![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) [![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)
![Project status](https://img.shields.io/badge/version-9.1.0-green.svg) ![Project status](https://img.shields.io/badge/version-9.1.1-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator) [![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9) [![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator) [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)

@ -1247,6 +1247,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} must contain a valid MAC address", translation: "{0} must contain a valid MAC address",
override: false, override: false,
}, },
{
tag: "iscolor",
translation: "{0} must be a valid color",
override: false,
},
} }
for _, t := range translations { for _, t := range translations {

@ -128,6 +128,7 @@ func TestTranslations(t *testing.T) {
IPAddrv6 string `validate:"ip6_addr"` IPAddrv6 string `validate:"ip6_addr"`
UinxAddr string `validate:"unix_addr"` // can't fail from within Go's net package currently, but maybe in the future UinxAddr string `validate:"unix_addr"` // can't fail from within Go's net package currently, but maybe in the future
MAC string `validate:"mac"` MAC string `validate:"mac"`
IsColor string `validate:"iscolor"`
} }
var test Test var test Test
@ -180,6 +181,10 @@ func TestTranslations(t *testing.T) {
ns string ns string
expected string expected string
}{ }{
{
ns: "Test.IsColor",
expected: "IsColor must be a valid color",
},
{ {
ns: "Test.MAC", ns: "Test.MAC",
expected: "MAC must contain a valid MAC address", expected: "MAC must contain a valid MAC address",

@ -310,6 +310,11 @@ OUTER:
v.misc = append(v.misc, '|') v.misc = append(v.misc, '|')
v.misc = append(v.misc, ct.tag...) v.misc = append(v.misc, ct.tag...)
if len(ct.param) > 0 {
v.misc = append(v.misc, '=')
v.misc = append(v.misc, ct.param...)
}
if ct.next == nil || ct.next.typeof != typeOr { // ct.typeof != typeOr if ct.next == nil || ct.next.typeof != typeOr { // ct.typeof != typeOr
// if we get here, no valid 'or' value and no more tags // if we get here, no valid 'or' value and no more tags
@ -373,6 +378,10 @@ OUTER:
v.flField = current v.flField = current
v.flParam = ct.param v.flParam = ct.param
// // report error interface functions need these
// v.ns = ns
// v.actualNs = structNs
if !ct.fn(v) { if !ct.fn(v) {
v.str1 = string(append(ns, cf.altName...)) v.str1 = string(append(ns, cf.altName...))

@ -5504,7 +5504,7 @@ func TestOrTag(t *testing.T) {
s = "this ain't right" s = "this ain't right"
errs = validate.Var(s, "rgb|rgba|len=10") errs = validate.Var(s, "rgb|rgba|len=10")
NotEqual(t, errs, nil) NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "rgb|rgba|len") AssertError(t, errs, "", "", "", "", "rgb|rgba|len=10")
s = "this is right" s = "this is right"
errs = validate.Var(s, "rgb|rgba|len=13") errs = validate.Var(s, "rgb|rgba|len=13")

Loading…
Cancel
Save