From 700e5f4b4623530c0f53832414efaecf1989c6e8 Mon Sep 17 00:00:00 2001 From: Anthony Olurin Date: Sun, 19 Apr 2015 00:01:15 -0400 Subject: [PATCH 1/4] Minor typos - Changes made to documentation and ReadME. --- README.md | 20 ++++++++++---------- doc.go | 28 ++++++++++++++-------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 0c1d430..dc95516 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@ Package validator [![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/validator.v5?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/validator.v5) Package validator implements value validations for structs and individual fields based on tags. -It is even capable of Cross Field and even Cross Field Cross Struct validation. +It is also capable of Cross Field and Cross Struct validations. Installation ============ -Just use go get. +Use go get. go get gopkg.in/bluesuncorp/validator.v5 @@ -17,22 +17,22 @@ or to update go get -u gopkg.in/bluesuncorp/validator.v5 -And then just import the package into your own code. +Then import the validator package into your own code. import "gopkg.in/bluesuncorp/validator.v5" -Usage -===== +Usage and documentation +======================= Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v5 for detailed usage docs. -Contributing -============ +How to Contribute +================= -There will be a development branch for each version of this package i.e. v1-development, please -make your pull requests against those branches. +There will be always be a development branch for each version i.e. `v1-development`. In order to contribute, +please make your pull requests against those branches. -If changes are breaking please create an issue, for discussion and create a pull request against +If changes made fails the test or are broken, please create an issue, for discussion and create a pull request against the highest development branch for example this package has a v1 and v1-development branch however, there will also be a v2-development brach even though v2 doesn't exist yet. diff --git a/doc.go b/doc.go index 6eaadb7..d27f152 100644 --- a/doc.go +++ b/doc.go @@ -1,5 +1,5 @@ /* -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. +Package validator implements value validations for structs and individual fields based on tags. It can also handle Cross Field and Cross Struct validation for nested structs. Validate @@ -250,55 +250,55 @@ Here is a list of the current built in validators: Validating by field validate.FieldWithValue(start, end, "ltefield") alpha - This validates that a strings value contains alpha characters only + This validates that a string value contains alpha characters only (Usage: alpha) alphanum - This validates that a strings value contains alphanumeric characters only + This validates that a string value contains alphanumeric characters only (Usage: alphanum) numeric - This validates that a strings value contains a basic numeric value. + This validates that a string value contains a basic numeric value. basic excludes exponents etc... (Usage: numeric) hexadecimal - This validates that a strings value contains a valid hexadecimal. + This validates that a string value contains a valid hexadecimal. (Usage: hexadecimal) hexcolor - This validates that a strings value contains a valid hex color including + This validates that a string value contains a valid hex color including hashtag (#) (Usage: hexcolor) rgb - This validates that a strings value contains a valid rgb color + This validates that a string value contains a valid rgb color (Usage: rgb) rgba - This validates that a strings value contains a valid rgba color + This validates that a string value contains a valid rgba color (Usage: rgba) hsl - This validates that a strings value contains a valid hsl color + This validates that a string value contains a valid hsl color (Usage: hsl) hsla - This validates that a strings value contains a valid hsla color + This validates that a string value contains a valid hsla color (Usage: hsla) email - This validates that a strings value contains a valid email + This validates that a string value contains a valid email This may not conform to all possibilities of any rfc standard, but neither does any email provider accept all posibilities... (Usage: email) url - This validates that a strings value contains a valid url + This validates that a string value contains a valid url This will accept any url the golang request uri accepts but must contain a schema for example http:// or rtmp:// (Usage: url) uri - This validates that a strings value contains a valid uri + This validates that a string value contains a valid uri This will accept any uri the golang request uri accepts (Usage: uri) Validator notes: @@ -314,7 +314,7 @@ Validator notes: used within the validator function and even be precompiled for better efficiency within regexes.go. - And the best reason, you can sumit a pull request and we can keep on adding to the + And the best reason, you can submit a pull request and we can keep on adding to the validation library of this package! Panics From 4c73ffba66ac6e8ecc1c9446520defc5aeea27c3 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 19 Apr 2015 09:27:13 -0400 Subject: [PATCH 2/4] update verbiage after pull request --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index dc95516..e5b52ba 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Installation Use go get. - go get gopkg.in/bluesuncorp/validator.v5 + go get -u gopkg.in/bluesuncorp/validator.v5 or to update @@ -29,12 +29,12 @@ Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v5 for detailed usage How to Contribute ================= -There will be always be a development branch for each version i.e. `v1-development`. In order to contribute, +There will always be a development branch for each version i.e. `v1-development`. In order to contribute, please make your pull requests against those branches. -If changes made fails the test or are broken, please create an issue, for discussion and create a pull request against -the highest development branch for example this package has a v1 and v1-development branch -however, there will also be a v2-development brach even though v2 doesn't exist yet. +If the changes being proposed or requested are breaking changes, please create an issue, for discussion +or create a pull request against the highest development branch for example this package has a +v1 and v1-development branch however, there will also be a v2-development brach even though v2 doesn't exist yet. I strongly encourage everyone whom creates a custom validation function to contribute them and help make this package even better. From 1d008b4ad28700f4eed062140d4597d56240b88f Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 19 Apr 2015 10:13:14 -0400 Subject: [PATCH 3/4] add base64 validate as requested in issue #30 --- baked_in.go | 5 +++++ regexes.go | 2 ++ validator_test.go | 28 +++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/baked_in.go b/baked_in.go index 4a3e7ee..b733fd0 100644 --- a/baked_in.go +++ b/baked_in.go @@ -37,6 +37,11 @@ var BakedInValidators = map[string]Func{ "email": isEmail, "url": isURL, "uri": isURI, + "base64": isBase64, +} + +func isBase64(top interface{}, current interface{}, field interface{}, param string) bool { + return matchesRegex(base64Regex, field) } func isURI(top interface{}, current interface{}, field interface{}, param string) bool { diff --git a/regexes.go b/regexes.go index 8107a21..e5ed0fa 100644 --- a/regexes.go +++ b/regexes.go @@ -14,6 +14,7 @@ const ( hslRegexString = "^hsl\\(\\s*(0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*((0|[1-9]\\d?|100)%)\\s*,\\s*((0|[1-9]\\d?|100)%)\\s*\\)$" hslaRegexString = "^hsla\\(\\s*(0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*((0|[1-9]\\d?|100)%)\\s*,\\s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DF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$" + base64RegexString = "(?:^(?:[A-Za-z0-9+\\/]{4}\\n?)*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=)$)" ) var ( @@ -28,6 +29,7 @@ var ( hslRegex = regexp.MustCompile(hslRegexString) hslaRegex = regexp.MustCompile(hslaRegexString) emailRegex = regexp.MustCompile(emailRegexString) + base64Regex = regexp.MustCompile(base64RegexString) ) func matchesRegex(regex *regexp.Regexp, field interface{}) bool { diff --git a/validator_test.go b/validator_test.go index b710730..fce8777 100644 --- a/validator_test.go +++ b/validator_test.go @@ -6,10 +6,16 @@ import ( "testing" "time" - "gopkg.in/bluesuncorp/validator.v5" + // "gopkg.in/bluesuncorp/validator.v5" + "github.com/joeybloggs/validator" . "gopkg.in/check.v1" ) +// NOTES: +// - Run "go test" to run tests +// - Run "gocov test | gocov report" to report on test converage by file +// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called + type I interface { Foo() string } @@ -137,6 +143,26 @@ func isEqualFunc(val interface{}, current interface{}, field interface{}, param return current.(string) == field.(string) } +func (ms *MySuite) TestBase64Validation(c *C) { + + s := "dW5pY29ybg==" + + err := validate.Field(s, "base64") + c.Assert(err, IsNil) + + s = "dGhpIGlzIGEgdGVzdCBiYXNlNjQ=" + err = validate.Field(s, "base64") + c.Assert(err, IsNil) + + s = "" + err = validate.Field(s, "base64") + c.Assert(err, NotNil) + + s = "dW5pY29ybg== foo bar" + err = validate.Field(s, "base64") + c.Assert(err, NotNil) +} + func (ms *MySuite) TestStructOnlyValidation(c *C) { type Inner struct { From e530eb5470d4948e37dd17e257227f8adb752e22 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 19 Apr 2015 10:21:03 -0400 Subject: [PATCH 4/4] add documentation for new base64 validator --- doc.go | 8 ++++++++ validator_test.go | 3 +-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc.go b/doc.go index d27f152..62b3eb9 100644 --- a/doc.go +++ b/doc.go @@ -292,15 +292,23 @@ Here is a list of the current built in validators: This may not conform to all possibilities of any rfc standard, but neither does any email provider accept all posibilities... (Usage: email) + url This validates that a string value contains a valid url This will accept any url the golang request uri accepts but must contain a schema for example http:// or rtmp:// (Usage: url) + uri This validates that a string value contains a valid uri This will accept any uri the golang request uri accepts (Usage: uri) + base64 + This validates that a string value contains a valid base64 value. + Although an empty string is valid base64 this will report an empty string + as an error, if you wish to accept an empty string as valid you can use + this with the omitempty tag. (Usage: base64) + Validator notes: regex diff --git a/validator_test.go b/validator_test.go index fce8777..55c5cb6 100644 --- a/validator_test.go +++ b/validator_test.go @@ -6,8 +6,7 @@ import ( "testing" "time" - // "gopkg.in/bluesuncorp/validator.v5" - "github.com/joeybloggs/validator" + "gopkg.in/bluesuncorp/validator.v5" . "gopkg.in/check.v1" )