Skip to content

Commit 191b1ce

Browse files
Dean KarnDean Karn
authored andcommitted
v2 add cross field validation ability
1 parent 5a0fdab commit 191b1ce

File tree

5 files changed

+189
-46
lines changed

5 files changed

+189
-46
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package go-validate-yourself
22
================
3-
[![Build Status](https://travis-ci.org/joeybloggs/go-validate-yourself.svg?branch=v2-development)](https://travis-ci.org/joeybloggs/go-validate-yourself)
3+
[![Build Status](https://travis-ci.org/joeybloggs/go-validate-yourself.svg?branch=v2)](https://travis-ci.org/joeybloggs/go-validate-yourself)
44

55
Package validator implements value validations for structs and individual fields based on tags.
66

@@ -9,20 +9,20 @@ Installation
99

1010
Just use go get.
1111

12-
go get gopkg.in/joeybloggs/go-validate-yourself.v1
12+
go get gopkg.in/joeybloggs/go-validate-yourself.v2
1313

1414
or to update
1515

16-
go get -u gopkg.in/joeybloggs/go-validate-yourself.v1
16+
go get -u gopkg.in/joeybloggs/go-validate-yourself.v2
1717

1818
And then just import the package into your own code.
1919

20-
import "gopkg.in/joeybloggs/go-validate-yourself.v1"
20+
import "gopkg.in/joeybloggs/go-validate-yourself.v2"
2121

2222
Usage
2323
=====
2424

25-
Please see http://godoc.org/gopkg.in/joeybloggs/go-validate-yourself.v1 for detailed usage docs.
25+
Please see http://godoc.org/gopkg.in/joeybloggs/go-validate-yourself.v2 for detailed usage docs.
2626

2727
Contributing
2828
============

baked_in.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var BakedInValidators = map[string]ValidationFunc{
3333
"uri": isURI,
3434
}
3535

36-
func isURI(field interface{}, param string) bool {
36+
func isURI(val interface{}, field interface{}, param string) bool {
3737

3838
st := reflect.ValueOf(field)
3939

@@ -48,7 +48,7 @@ func isURI(field interface{}, param string) bool {
4848
}
4949
}
5050

51-
func isURL(field interface{}, param string) bool {
51+
func isURL(val interface{}, field interface{}, param string) bool {
5252

5353
st := reflect.ValueOf(field)
5454

@@ -72,7 +72,7 @@ func isURL(field interface{}, param string) bool {
7272
}
7373
}
7474

75-
func isEmail(field interface{}, param string) bool {
75+
func isEmail(val interface{}, field interface{}, param string) bool {
7676

7777
st := reflect.ValueOf(field)
7878

@@ -85,7 +85,7 @@ func isEmail(field interface{}, param string) bool {
8585
}
8686
}
8787

88-
func isHsla(field interface{}, param string) bool {
88+
func isHsla(val interface{}, field interface{}, param string) bool {
8989

9090
st := reflect.ValueOf(field)
9191

@@ -98,7 +98,7 @@ func isHsla(field interface{}, param string) bool {
9898
}
9999
}
100100

101-
func isHsl(field interface{}, param string) bool {
101+
func isHsl(val interface{}, field interface{}, param string) bool {
102102

103103
st := reflect.ValueOf(field)
104104

@@ -111,7 +111,7 @@ func isHsl(field interface{}, param string) bool {
111111
}
112112
}
113113

114-
func isRgba(field interface{}, param string) bool {
114+
func isRgba(val interface{}, field interface{}, param string) bool {
115115

116116
st := reflect.ValueOf(field)
117117

@@ -124,7 +124,7 @@ func isRgba(field interface{}, param string) bool {
124124
}
125125
}
126126

127-
func isRgb(field interface{}, param string) bool {
127+
func isRgb(val interface{}, field interface{}, param string) bool {
128128

129129
st := reflect.ValueOf(field)
130130

@@ -137,7 +137,7 @@ func isRgb(field interface{}, param string) bool {
137137
}
138138
}
139139

140-
func isHexcolor(field interface{}, param string) bool {
140+
func isHexcolor(val interface{}, field interface{}, param string) bool {
141141

142142
st := reflect.ValueOf(field)
143143

@@ -150,7 +150,7 @@ func isHexcolor(field interface{}, param string) bool {
150150
}
151151
}
152152

153-
func isHexadecimal(field interface{}, param string) bool {
153+
func isHexadecimal(val interface{}, field interface{}, param string) bool {
154154

155155
st := reflect.ValueOf(field)
156156

@@ -163,7 +163,7 @@ func isHexadecimal(field interface{}, param string) bool {
163163
}
164164
}
165165

166-
func isNumber(field interface{}, param string) bool {
166+
func isNumber(val interface{}, field interface{}, param string) bool {
167167

168168
st := reflect.ValueOf(field)
169169

@@ -176,7 +176,7 @@ func isNumber(field interface{}, param string) bool {
176176
}
177177
}
178178

179-
func isNumeric(field interface{}, param string) bool {
179+
func isNumeric(val interface{}, field interface{}, param string) bool {
180180

181181
st := reflect.ValueOf(field)
182182

@@ -189,7 +189,7 @@ func isNumeric(field interface{}, param string) bool {
189189
}
190190
}
191191

192-
func isAlphanum(field interface{}, param string) bool {
192+
func isAlphanum(val interface{}, field interface{}, param string) bool {
193193

194194
st := reflect.ValueOf(field)
195195

@@ -202,7 +202,7 @@ func isAlphanum(field interface{}, param string) bool {
202202
}
203203
}
204204

205-
func isAlpha(field interface{}, param string) bool {
205+
func isAlpha(val interface{}, field interface{}, param string) bool {
206206

207207
st := reflect.ValueOf(field)
208208

@@ -215,7 +215,7 @@ func isAlpha(field interface{}, param string) bool {
215215
}
216216
}
217217

218-
func hasValue(field interface{}, param string) bool {
218+
func hasValue(val interface{}, field interface{}, param string) bool {
219219

220220
st := reflect.ValueOf(field)
221221

@@ -229,7 +229,7 @@ func hasValue(field interface{}, param string) bool {
229229
}
230230
}
231231

232-
func isGte(field interface{}, param string) bool {
232+
func isGte(val interface{}, field interface{}, param string) bool {
233233

234234
st := reflect.ValueOf(field)
235235

@@ -265,7 +265,7 @@ func isGte(field interface{}, param string) bool {
265265
}
266266
}
267267

268-
func isGt(field interface{}, param string) bool {
268+
func isGt(val interface{}, field interface{}, param string) bool {
269269

270270
st := reflect.ValueOf(field)
271271

@@ -304,7 +304,7 @@ func isGt(field interface{}, param string) bool {
304304
// length tests whether a variable's length is equal to a given
305305
// value. For strings it tests the number of characters whereas
306306
// for maps and slices it tests the number of items.
307-
func hasLengthOf(field interface{}, param string) bool {
307+
func hasLengthOf(val interface{}, field interface{}, param string) bool {
308308

309309
st := reflect.ValueOf(field)
310310

@@ -344,12 +344,12 @@ func hasLengthOf(field interface{}, param string) bool {
344344
// number. For number types, it's a simple lesser-than test; for
345345
// strings it tests the number of characters whereas for maps
346346
// and slices it tests the number of items.
347-
func hasMinOf(field interface{}, param string) bool {
347+
func hasMinOf(val interface{}, field interface{}, param string) bool {
348348

349-
return isGte(field, param)
349+
return isGte(val, field, param)
350350
}
351351

352-
func isLte(field interface{}, param string) bool {
352+
func isLte(val interface{}, field interface{}, param string) bool {
353353

354354
st := reflect.ValueOf(field)
355355

@@ -385,7 +385,7 @@ func isLte(field interface{}, param string) bool {
385385
}
386386
}
387387

388-
func isLt(field interface{}, param string) bool {
388+
func isLt(val interface{}, field interface{}, param string) bool {
389389

390390
st := reflect.ValueOf(field)
391391

@@ -425,9 +425,9 @@ func isLt(field interface{}, param string) bool {
425425
// value. For numbers, it's a simple lesser-than test; for
426426
// strings it tests the number of characters whereas for maps
427427
// and slices it tests the number of items.
428-
func hasMaxOf(field interface{}, param string) bool {
428+
func hasMaxOf(val interface{}, field interface{}, param string) bool {
429429

430-
return isLte(field, param)
430+
return isLte(val, field, param)
431431
}
432432

433433
// asInt retuns the parameter as a int64

doc.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Custom Functions
8282
Custom functions can be added
8383
8484
//Structure
85-
func customFunc(field interface{}, param string) bool {
85+
func customFunc(val interface{}, field interface{}, param string) bool {
8686
8787
if whatever {
8888
return false
@@ -92,8 +92,34 @@ Custom functions can be added
9292
}
9393
9494
validator.AddFunction("custom tag name", customFunc)
95-
// NOTE: using the same tag name as an existing function
96-
// will overwrite the existing one
95+
// NOTES: using the same tag name as an existing function
96+
// will overwrite the existing one
97+
98+
Cross Field Validation
99+
100+
Cross Field Validation can be implemented, for example Start & End Date range validation
101+
102+
// NOTE: when calling validator.validateStruct(val) val will be the top level struct passed
103+
// into the function
104+
// when calling validator.ValidateFieldByTagAndValue(val, field, tag) val will be
105+
// whatever you pass, struct, field...
106+
// when calling validator.ValidateFieldByTag(field, tag) val will be nil
107+
//
108+
// Because of the specific requirements and field names within each persons project that
109+
// uses this library it is unlikely that any baked in function for this type of validation
110+
// would be added, but you can add your own custom ones and keep all your validation logic
111+
// in one place.
112+
113+
func isDateRangeValid(val interface{}, field interface{}, param string) bool {
114+
115+
myStruct := val.(myStructType)
116+
117+
if myStruct.Start.After(field.(time.Time)) {
118+
return false
119+
}
120+
121+
return true
122+
}
97123
98124
Custom Tag Name
99125

0 commit comments

Comments
 (0)