Skip to content

Commit b1f51f3

Browse files
author
Dean Karn
authored
Correct consequtive or logic (#330)
1 parent 61caf9d commit b1f51f3

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package validator
22
================
33
<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)
4-
![Project status](https://img.shields.io/badge/version-9.9.0-green.svg)
4+
![Project status](https://img.shields.io/badge/version-9.9.1-green.svg)
55
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
66
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)

cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ type cTag struct {
9696
next *cTag
9797
hasTag bool
9898
hasAlias bool
99+
hasParam bool // true if parameter used eg. eq= where the equal sign has been set
100+
isBlockEnd bool // indicates the current tag represents the last validation in the block
99101
fn FuncCtx
100102
}
101103

@@ -291,6 +293,7 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s
291293
current.next = &cTag{aliasTag: alias, actualAliasTag: current.actualAliasTag, hasAlias: hasAlias, hasTag: true}
292294
current = current.next
293295
}
296+
current.hasParam = len(vals) > 1
294297

295298
current.tag = vals[0]
296299
if len(current.tag) == 0 {
@@ -309,6 +312,7 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s
309312
current.param = strings.Replace(strings.Replace(vals[1], utf8HexComma, ",", -1), utf8Pipe, "|", -1)
310313
}
311314
}
315+
current.isBlockEnd = true
312316
}
313317
}
314318

validator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,13 @@ OUTER:
378378
v.misc = append(v.misc, '|')
379379
v.misc = append(v.misc, ct.tag...)
380380

381-
if len(ct.param) > 0 {
381+
if ct.hasParam {
382382
v.misc = append(v.misc, '=')
383383
v.misc = append(v.misc, ct.param...)
384384
}
385385

386-
if ct.next == nil || ct.next.typeof != typeOr { // ct.typeof != typeOr
386+
if ct.isBlockEnd || ct.next == nil {
387387
// if we get here, no valid 'or' value and no more tags
388-
389388
v.str1 = string(append(ns, cf.altName...))
390389

391390
if v.v.hasTagNameFunc {

validator_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5585,6 +5585,13 @@ func TestOrTag(t *testing.T) {
55855585
errs = validate.Var(s, "omitempty,rgb|rgba")
55865586
Equal(t, errs, nil)
55875587

5588+
s = "green"
5589+
errs = validate.Var(s, "eq=|eq=blue,rgb|rgba") //should fail on first validation block
5590+
NotEqual(t, errs, nil)
5591+
ve := errs.(ValidationErrors)
5592+
Equal(t, len(ve), 1)
5593+
Equal(t, ve[0].Tag(), "eq=|eq=blue")
5594+
55885595
s = "this is right, but a blank or isn't"
55895596

55905597
PanicMatches(t, func() { validate.Var(s, "rgb||len=13") }, "Invalid validation tag on field ''")

0 commit comments

Comments
 (0)