Skip to content

Commit 9d1ffae

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #28 from joeybloggs/v5
changes in preparation for package rename to validator
2 parents 47a6634 + 7ac98be commit 9d1ffae

File tree

5 files changed

+386
-385
lines changed

5 files changed

+386
-385
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
Package go-validate-yourself
22
================
3-
[![Build Status](https://travis-ci.org/bluesuncorp/go-validate-yourself.svg?branch=v4)](https://travis-ci.org/bluesuncorp/go-validate-yourself)
4-
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v4?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v4)
3+
[![Build Status](https://travis-ci.org/bluesuncorp/go-validate-yourself.svg?branch=v5)](https://travis-ci.org/bluesuncorp/go-validate-yourself)
4+
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v5?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v5)
55

66
Package validator implements value validations for structs and individual fields based on tags.
7+
It is even capable of Cross Field and even Cross Field Cross Struct validation.
78

89
Installation
910
============
1011

1112
Just use go get.
1213

13-
go get gopkg.in/bluesuncorp/go-validate-yourself.v4
14+
go get gopkg.in/bluesuncorp/go-validate-yourself.v5
1415

1516
or to update
1617

17-
go get -u gopkg.in/bluesuncorp/go-validate-yourself.v4
18+
go get -u gopkg.in/bluesuncorp/go-validate-yourself.v5
1819

1920
And then just import the package into your own code.
2021

21-
import "gopkg.in/bluesuncorp/go-validate-yourself.v4"
22+
import "gopkg.in/bluesuncorp/go-validate-yourself.v5"
2223

2324
Usage
2425
=====
2526

26-
Please see http://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v4 for detailed usage docs.
27+
Please see http://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v5 for detailed usage docs.
2728

2829
Contributing
2930
============

baked_in.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// BakedInValidators is the default map of ValidationFunc
1212
// you can add, remove or even replace items to suite your needs,
1313
// or even disregard and use your own map if so desired.
14-
var BakedInValidators = map[string]ValidationFunc{
14+
var BakedInValidators = map[string]Func{
1515
"required": hasValue,
1616
"len": hasLengthOf,
1717
"min": hasMinOf,

doc.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
Package 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
1111
A 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
4040
Error 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
4949
intended 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
6969
The 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
9696
Cross Field Validation
9797
9898
Cross 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
*/
334334
package validator

0 commit comments

Comments
 (0)