From b1f51f36f1c98cc97f777d6fc9d4b05eaa0cabb5 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Wed, 13 Dec 2017 08:54:38 -0800 Subject: [PATCH] Correct consequtive or logic (#330) --- README.md | 2 +- cache.go | 4 ++++ validator.go | 5 ++--- validator_test.go | 7 +++++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0259370..f88aa9d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Package validator ================ [![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.9.0-green.svg) +![Project status](https://img.shields.io/badge/version-9.9.1-green.svg) [![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) [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator) diff --git a/cache.go b/cache.go index 8c59e16..1706274 100644 --- a/cache.go +++ b/cache.go @@ -96,6 +96,8 @@ type cTag struct { next *cTag hasTag bool hasAlias bool + hasParam bool // true if parameter used eg. eq= where the equal sign has been set + isBlockEnd bool // indicates the current tag represents the last validation in the block fn FuncCtx } @@ -291,6 +293,7 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s current.next = &cTag{aliasTag: alias, actualAliasTag: current.actualAliasTag, hasAlias: hasAlias, hasTag: true} current = current.next } + current.hasParam = len(vals) > 1 current.tag = vals[0] if len(current.tag) == 0 { @@ -309,6 +312,7 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s current.param = strings.Replace(strings.Replace(vals[1], utf8HexComma, ",", -1), utf8Pipe, "|", -1) } } + current.isBlockEnd = true } } diff --git a/validator.go b/validator.go index f180a9c..10ed593 100644 --- a/validator.go +++ b/validator.go @@ -378,14 +378,13 @@ OUTER: v.misc = append(v.misc, '|') v.misc = append(v.misc, ct.tag...) - if len(ct.param) > 0 { + if ct.hasParam { 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.isBlockEnd || ct.next == nil { // if we get here, no valid 'or' value and no more tags - v.str1 = string(append(ns, cf.altName...)) if v.v.hasTagNameFunc { diff --git a/validator_test.go b/validator_test.go index 7d085e8..752300f 100644 --- a/validator_test.go +++ b/validator_test.go @@ -5585,6 +5585,13 @@ func TestOrTag(t *testing.T) { errs = validate.Var(s, "omitempty,rgb|rgba") Equal(t, errs, nil) + s = "green" + errs = validate.Var(s, "eq=|eq=blue,rgb|rgba") //should fail on first validation block + NotEqual(t, errs, nil) + ve := errs.(ValidationErrors) + Equal(t, len(ve), 1) + Equal(t, ve[0].Tag(), "eq=|eq=blue") + s = "this is right, but a blank or isn't" PanicMatches(t, func() { validate.Var(s, "rgb||len=13") }, "Invalid validation tag on field ''")