@@ -18,7 +18,10 @@ import (
1818)
1919
2020const (
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.
120119func 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
133127func (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
143132func (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
170153func (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
267244func (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
279250func (v * Validator ) ValidateFieldByTagAndValue (val interface {}, f interface {}, tag string ) * FieldValidationError {
280251
@@ -284,7 +255,7 @@ func (v *Validator) ValidateFieldByTagAndValue(val interface{}, f interface{}, t
284255func (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
357328func (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