Skip to content

Commit cfea08f

Browse files
Dean KarnDean Karn
authored andcommitted
issue-#14
remove internal validator.
1 parent 93115af commit cfea08f

File tree

4 files changed

+242
-278
lines changed

4 files changed

+242
-278
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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=v3-development)](https://travis-ci.org/joeybloggs/go-validate-yourself)
3+
[![Build Status](https://travis-ci.org/joeybloggs/go-validate-yourself.svg?branch=v3)](https://travis-ci.org/joeybloggs/go-validate-yourself)
44

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

doc.go

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ Package validator implements value validations for structs and individual fields
33
44
Built In Validator
55
6-
The package contains a built in Validator instance for use,
7-
but you may also create a new instance if needed.
6+
v3 no longer contains a built in Validator instance.
87
9-
// built in
10-
errs := validator.ValidateStruct(//your struct)
11-
valErr := validator.ValidateFieldByTag(field, "omitempty,min=1,max=10")
8+
myValidator = validator.New("validate", validator.BakedInFunctions)
129
13-
// new
14-
newValidator = validator.New("struct tag name", validator.BakedInFunctions)
10+
errs := myValidator.ValidateStruct(//your struct)
11+
valErr := myValidator.ValidateFieldByTag(field, "omitempty,min=1,max=10")
1512
1613
A simple example usage:
1714
@@ -32,7 +29,7 @@ A simple example usage:
3229
// errs will contain a hierarchical list of errors
3330
// using the StructValidationErrors struct
3431
// or nil if no errors exist
35-
errs := validator.ValidateStruct(user)
32+
errs := myValidator.ValidateStruct(user)
3633
3734
// in this case 1 error Name is required
3835
errs.Struct will be "User"
@@ -68,7 +65,7 @@ I needed to know the field and what validation failed so that I could provide an
6865
return "Translated string based on field"
6966
}
7067
71-
The hierarchical structure is hard to work with sometimes.. Agreed Flatten function to the rescue!
68+
The hierarchical error structure is hard to work with sometimes.. Agreed Flatten function to the rescue!
7269
Flatten will return a map of FieldValidationError's but the field name will be namespaced.
7370
7471
// if UserDetail Details field failed validation
@@ -91,24 +88,24 @@ Custom functions can be added
9188
return true
9289
}
9390
94-
validator.AddFunction("custom tag name", customFunc)
91+
myValidator.AddFunction("custom tag name", customFunc)
9592
// NOTES: using the same tag name as an existing function
9693
// will overwrite the existing one
9794
9895
Cross Field Validation
9996
10097
Cross Field Validation can be implemented, for example Start & End Date range validation
10198
102-
// NOTE: when calling validator.validateStruct(val) val will be the top level struct passed
99+
// NOTE: when calling myValidator.validateStruct(val) val will be the top level struct passed
103100
// into the function
104-
// when calling validator.ValidateFieldByTagAndValue(val, field, tag) val will be
101+
// when calling myValidator.ValidateFieldByTagAndValue(val, field, tag) val will be
105102
// whatever you pass, struct, field...
106-
// when calling validator.ValidateFieldByTag(field, tag) val will be nil
103+
// when calling myValidator.ValidateFieldByTag(field, tag) val will be nil
107104
//
108105
// 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.
106+
// uses this library it is likely that custom functions will need to be created.
107+
// however there are some build in Generic Cross Field validation, see Baked In Validators and
108+
// Tags below
112109
113110
func isDateRangeValid(val interface{}, field interface{}, param string) bool {
114111
@@ -121,12 +118,6 @@ Cross Field Validation can be implemented, for example Start & End Date range va
121118
return true
122119
}
123120
124-
Custom Tag Name
125-
126-
A custom tag name can be set to avoid conficts, or just have a shorter name
127-
128-
validator.SetTag("valid")
129-
130121
Multiple Validators
131122
132123
Multiple validators on a field will process in the order defined
@@ -325,6 +316,6 @@ This package panics when bad input is provided, this is by design, bad code like
325316
TestField: "Test"
326317
}
327318
328-
validator.ValidateStruct(t) // this will panic
319+
myValidator.ValidateStruct(t) // this will panic
329320
*/
330321
package validator

validator.go

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import (
1818
)
1919

2020
const (
21-
defaultTagName = "validate"
21+
tagSeparator = ","
22+
orSeparator = "|"
23+
noValidationTag = "-"
24+
tagKeySeparator = "="
2225
omitempty = "omitempty"
2326
validationFieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n"
2427
validationStructErrMsg = "Struct:%s\n"
@@ -111,10 +114,6 @@ type Validator struct {
111114
validationFuncs map[string]ValidationFunc
112115
}
113116

114-
// var bakedInValidators = map[string]ValidationFunc{}
115-
116-
var internalValidator = NewValidator(defaultTagName, BakedInValidators)
117-
118117
// NewValidator creates a new Validator instance
119118
// NOTE: it is not necessary to create a new validator as the internal one will do in 99.9% of cases, but the option is there.
120119
func NewValidator(tagName string, funcs map[string]ValidationFunc) *Validator {
@@ -124,21 +123,11 @@ func NewValidator(tagName string, funcs map[string]ValidationFunc) *Validator {
124123
}
125124
}
126125

127-
// SetTag sets the baked in Validator's tagName to one of your choosing
128-
func SetTag(tagName string) {
129-
internalValidator.SetTag(tagName)
130-
}
131-
132126
// SetTag sets tagName of the Validator to one of your choosing
133127
func (v *Validator) SetTag(tagName string) {
134128
v.tagName = tagName
135129
}
136130

137-
// AddFunction adds a ValidationFunc to the baked in Validator's map of validators denoted by the key
138-
func AddFunction(key string, f ValidationFunc) error {
139-
return internalValidator.AddFunction(key, f)
140-
}
141-
142131
// AddFunction adds a ValidationFunc to a Validator's map of validators denoted by the key
143132
func (v *Validator) AddFunction(key string, f ValidationFunc) error {
144133

@@ -160,12 +149,6 @@ func (v *Validator) AddFunction(key string, f ValidationFunc) error {
160149
return nil
161150
}
162151

163-
// ValidateStruct validates a struct and returns a struct containing the errors
164-
func ValidateStruct(s interface{}) *StructValidationErrors {
165-
166-
return internalValidator.ValidateStruct(s)
167-
}
168-
169152
// ValidateStruct validates a struct and returns a struct containing the errors
170153
func (v *Validator) ValidateStruct(s interface{}) *StructValidationErrors {
171154

@@ -206,7 +189,7 @@ func (v *Validator) validateStructRecursive(top interface{}, s interface{}) *Str
206189

207190
tag := typeField.Tag.Get(v.tagName)
208191

209-
if tag == "-" {
192+
if tag == noValidationTag {
210193
continue
211194
}
212195

@@ -257,24 +240,12 @@ func (v *Validator) validateStructRecursive(top interface{}, s interface{}) *Str
257240
return validationErrors
258241
}
259242

260-
// ValidateFieldByTag allows validation of a single field with the internal validator, still using tag style validation to check multiple errors
261-
func ValidateFieldByTag(f interface{}, tag string) *FieldValidationError {
262-
263-
return internalValidator.ValidateFieldByTag(f, tag)
264-
}
265-
266243
// ValidateFieldByTag allows validation of a single field, still using tag style validation to check multiple errors
267244
func (v *Validator) ValidateFieldByTag(f interface{}, tag string) *FieldValidationError {
268245

269246
return v.ValidateFieldByTagAndValue(nil, f, tag)
270247
}
271248

272-
// ValidateFieldByTagAndValue allows validation of a single field with the internal validator, still using tag style validation to check multiple errors
273-
func ValidateFieldByTagAndValue(val interface{}, f interface{}, tag string) *FieldValidationError {
274-
275-
return internalValidator.ValidateFieldByTagAndValue(val, f, tag)
276-
}
277-
278249
// ValidateFieldByTagAndValue allows validation of a single field, still using tag style validation to check multiple errors
279250
func (v *Validator) ValidateFieldByTagAndValue(val interface{}, f interface{}, tag string) *FieldValidationError {
280251

@@ -284,7 +255,7 @@ func (v *Validator) ValidateFieldByTagAndValue(val interface{}, f interface{}, t
284255
func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interface{}, name string, tag string) *FieldValidationError {
285256

286257
// This is a double check if coming from ValidateStruct but need to be here in case function is called directly
287-
if tag == "-" {
258+
if tag == noValidationTag {
288259
return nil
289260
}
290261

@@ -312,11 +283,11 @@ func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interfa
312283

313284
var valErr *FieldValidationError
314285
var err error
315-
valTags := strings.Split(tag, ",")
286+
valTags := strings.Split(tag, tagSeparator)
316287

317288
for _, valTag := range valTags {
318289

319-
orVals := strings.Split(valTag, "|")
290+
orVals := strings.Split(valTag, orSeparator)
320291

321292
if len(orVals) > 1 {
322293

@@ -330,11 +301,11 @@ func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interfa
330301
return nil
331302
}
332303

333-
errTag += "|" + valErr.ErrorTag
304+
errTag += orSeparator + valErr.ErrorTag
334305

335306
}
336307

337-
errTag = strings.TrimLeft(errTag, "|")
308+
errTag = strings.TrimLeft(errTag, orSeparator)
338309

339310
valErr.ErrorTag = errTag
340311
valErr.Kind = fieldKind
@@ -356,7 +327,7 @@ func (v *Validator) validateFieldByNameAndTagAndValue(val interface{}, f interfa
356327

357328
func (v *Validator) validateFieldByNameAndSingleTag(val interface{}, f interface{}, name string, valTag string) (*FieldValidationError, error) {
358329

359-
vals := strings.Split(valTag, "=")
330+
vals := strings.Split(valTag, tagKeySeparator)
360331
key := strings.Trim(vals[0], " ")
361332

362333
if len(key) == 0 {

0 commit comments

Comments
 (0)