From 1d008b4ad28700f4eed062140d4597d56240b88f Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Sun, 19 Apr 2015 10:13:14 -0400 Subject: [PATCH] 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*((0|[1-9]\\d?|100)%)\\s*,\\s*((0.[1-9]*)|[01])\\s*\\)$" emailRegexString = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\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}])|(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\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 {