Skip to content

Commit 765fb71

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #32 from joeybloggs/v5-development
V5 development
2 parents d400cda + e530eb5 commit 765fb71

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Installation
1111

1212
Use go get.
1313

14-
go get gopkg.in/bluesuncorp/validator.v5
14+
go get -u gopkg.in/bluesuncorp/validator.v5
1515

1616
or to update
1717

@@ -29,12 +29,12 @@ Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v5 for detailed usage
2929
How to Contribute
3030
=================
3131

32-
There will be always be a development branch for each version i.e. `v1-development`. In order to contribute,
32+
There will always be a development branch for each version i.e. `v1-development`. In order to contribute,
3333
please make your pull requests against those branches.
3434

35-
If changes made fails the test or are broken, please create an issue, for discussion and create a pull request against
36-
the highest development branch for example this package has a v1 and v1-development branch
37-
however, there will also be a v2-development brach even though v2 doesn't exist yet.
35+
If the changes being proposed or requested are breaking changes, please create an issue, for discussion
36+
or create a pull request against the highest development branch for example this package has a
37+
v1 and v1-development branch however, there will also be a v2-development brach even though v2 doesn't exist yet.
3838

3939
I strongly encourage everyone whom creates a custom validation function to contribute them and
4040
help make this package even better.

baked_in.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ var BakedInValidators = map[string]Func{
3737
"email": isEmail,
3838
"url": isURL,
3939
"uri": isURI,
40+
"base64": isBase64,
41+
}
42+
43+
func isBase64(top interface{}, current interface{}, field interface{}, param string) bool {
44+
return matchesRegex(base64Regex, field)
4045
}
4146

4247
func isURI(top interface{}, current interface{}, field interface{}, param string) bool {

doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,23 @@ Here is a list of the current built in validators:
292292
This may not conform to all possibilities of any rfc standard, but neither
293293
does any email provider accept all posibilities...
294294
(Usage: email)
295+
295296
url
296297
This validates that a string value contains a valid url
297298
This will accept any url the golang request uri accepts but must contain
298299
a schema for example http:// or rtmp://
299300
(Usage: url)
301+
300302
uri
301303
This validates that a string value contains a valid uri
302304
This will accept any uri the golang request uri accepts (Usage: uri)
303305
306+
base64
307+
This validates that a string value contains a valid base64 value.
308+
Although an empty string is valid base64 this will report an empty string
309+
as an error, if you wish to accept an empty string as valid you can use
310+
this with the omitempty tag. (Usage: base64)
311+
304312
Validator notes:
305313
306314
regex

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
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*\\)$"
1515
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*\\)$"
1616
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}])))\\.?$"
17+
base64RegexString = "(?:^(?:[A-Za-z0-9+\\/]{4}\\n?)*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=)$)"
1718
)
1819

1920
var (
@@ -28,6 +29,7 @@ var (
2829
hslRegex = regexp.MustCompile(hslRegexString)
2930
hslaRegex = regexp.MustCompile(hslaRegexString)
3031
emailRegex = regexp.MustCompile(emailRegexString)
32+
base64Regex = regexp.MustCompile(base64RegexString)
3133
)
3234

3335
func matchesRegex(regex *regexp.Regexp, field interface{}) bool {

validator_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010
. "gopkg.in/check.v1"
1111
)
1212

13+
// NOTES:
14+
// - Run "go test" to run tests
15+
// - Run "gocov test | gocov report" to report on test converage by file
16+
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
17+
1318
type I interface {
1419
Foo() string
1520
}
@@ -137,6 +142,26 @@ func isEqualFunc(val interface{}, current interface{}, field interface{}, param
137142
return current.(string) == field.(string)
138143
}
139144

145+
func (ms *MySuite) TestBase64Validation(c *C) {
146+
147+
s := "dW5pY29ybg=="
148+
149+
err := validate.Field(s, "base64")
150+
c.Assert(err, IsNil)
151+
152+
s = "dGhpIGlzIGEgdGVzdCBiYXNlNjQ="
153+
err = validate.Field(s, "base64")
154+
c.Assert(err, IsNil)
155+
156+
s = ""
157+
err = validate.Field(s, "base64")
158+
c.Assert(err, NotNil)
159+
160+
s = "dW5pY29ybg== foo bar"
161+
err = validate.Field(s, "base64")
162+
c.Assert(err, NotNil)
163+
}
164+
140165
func (ms *MySuite) TestStructOnlyValidation(c *C) {
141166

142167
type Inner struct {

0 commit comments

Comments
 (0)