11/*
22Package validator implements value validations for structs and individual fields based on tags. It can also handle Cross Field validation and even Cross Field Cross Struct validation for nested structs.
33
4- Built In Validator
4+ Validate
55
6- myValidator = validator.NewValidator ("validate", validator.BakedInValidators)
6+ validate : = validator.New ("validate", validator.BakedInValidators)
77
8- errs := myValidator.ValidateStruct (//your struct)
9- valErr := myValidator.ValidateFieldByTag (field, "omitempty,min=1,max=10")
8+ errs := validate.Struct (//your struct)
9+ valErr := validate.Field (field, "omitempty,min=1,max=10")
1010
1111A simple example usage:
1212
@@ -25,17 +25,17 @@ A simple example usage:
2525 }
2626
2727 // errs will contain a hierarchical list of errors
28- // using the StructValidationErrors struct
28+ // using the StructErrors struct
2929 // or nil if no errors exist
30- errs := myValidator.ValidateStruct (user)
30+ errs := validate.Struct (user)
3131
3232 // in this case 1 error Name is required
3333 errs.Struct will be "User"
3434 errs.StructErrors will be empty <-- fields that were structs
35- errs.Errors will have 1 error of type FieldValidationError
35+ errs.Errors will have 1 error of type FieldError
3636
3737 NOTE: Anonymous Structs - they don't have names so expect the Struct name
38- within StructValidationErrors to be blank.
38+ within StructErrors to be blank.
3939
4040Error Handling
4141
@@ -45,7 +45,7 @@ The error can be used like so
4545 fieldErr.Field // "Name"
4646 fieldErr.ErrorTag // "required"
4747
48- Both StructValidationErrors and FieldValidationError implement the Error interface but it's
48+ Both StructErrors and FieldError implement the Error interface but it's
4949intended use is for development + debugging, not a production error message.
5050
5151 fieldErr.Error() // Field validation for "Name" failed on the "required" tag
@@ -67,7 +67,7 @@ I needed to know the field and what validation failed so that I could provide an
6767 }
6868
6969The hierarchical error structure is hard to work with sometimes.. Agreed Flatten function to the rescue!
70- Flatten will return a map of FieldValidationError 's but the field name will be namespaced.
70+ Flatten will return a map of FieldError 's but the field name will be namespaced.
7171
7272 // if UserDetail Details field failed validation
7373 Field will be "Sub.Details"
@@ -89,19 +89,19 @@ Custom functions can be added
8989 return true
9090 }
9191
92- myValidator .AddFunction("custom tag name", customFunc)
92+ validate .AddFunction("custom tag name", customFunc)
9393 // NOTES: using the same tag name as an existing function
9494 // will overwrite the existing one
9595
9696Cross Field Validation
9797
9898Cross Field Validation can be implemented, for example Start & End Date range validation
9999
100- // NOTE: when calling myValidator.validateStruct (val) val will be the top level struct passed
100+ // NOTE: when calling validate.Struct (val) val will be the top level struct passed
101101 // into the function
102- // when calling myValidator.ValidateFieldByTagAndValue (val, field, tag) val will be
102+ // when calling validate.FieldWithValue (val, field, tag) val will be
103103 // whatever you pass, struct, field...
104- // when calling myValidator.ValidateFieldByTag (field, tag) val will be nil
104+ // when calling validate.Field (field, tag) val will be nil
105105 //
106106 // Because of the specific requirements and field names within each persons project that
107107 // uses this library it is likely that custom functions will need to be created for your
@@ -225,29 +225,29 @@ Here is a list of the current built in validators:
225225 Only valid for Numbers and time.Time types, this will validate the field value
226226 against another fields value either within a struct or passed in field.
227227 usage examples are for validation of a Start and End date:
228- Validation on End field using ValidateByStruct Usage(gtfield=Start)
229- Validating by field ValidateFieldByTagAndValue (start, end, "gtfield")
228+ Validation on End field using validate.Struct Usage(gtfield=Start)
229+ Validating by field validate.FieldWithValue (start, end, "gtfield")
230230
231231 gtefield
232232 Only valid for Numbers and time.Time types, this will validate the field value
233233 against another fields value either within a struct or passed in field.
234234 usage examples are for validation of a Start and End date:
235- Validation on End field using ValidateByStruct Usage(gtefield=Start)
236- Validating by field ValidateFieldByTagAndValue (start, end, "gtefield")
235+ Validation on End field using validate.Struct Usage(gtefield=Start)
236+ Validating by field validate.FieldWithValue (start, end, "gtefield")
237237
238238 ltfield
239239 Only valid for Numbers and time.Time types, this will validate the field value
240240 against another fields value either within a struct or passed in field.
241241 usage examples are for validation of a Start and End date:
242- Validation on End field using ValidateByStruct Usage(ltfield=Start)
243- Validating by field ValidateFieldByTagAndValue (start, end, "ltfield")
242+ Validation on End field using validate.Struct Usage(ltfield=Start)
243+ Validating by field validate.FieldWithValue (start, end, "ltfield")
244244
245245 ltefield
246246 Only valid for Numbers and time.Time types, this will validate the field value
247247 against another fields value either within a struct or passed in field.
248248 usage examples are for validation of a Start and End date:
249- Validation on End field using ValidateByStruct Usage(ltefield=Start)
250- Validating by field ValidateFieldByTagAndValue (start, end, "ltefield")
249+ Validation on End field using validate.Struct Usage(ltefield=Start)
250+ Validating by field validate.FieldWithValue (start, end, "ltefield")
251251
252252 alpha
253253 This validates that a strings value contains alpha characters only
@@ -329,6 +329,6 @@ This package panics when bad input is provided, this is by design, bad code like
329329 TestField: "Test"
330330 }
331331
332- myValidator.ValidateStruct (t) // this will panic
332+ validate.Struct (t) // this will panic
333333*/
334334package validator
0 commit comments