Skip to content

Commit fa8e066

Browse files
Dean KarnDean Karn
Dean Karn
authored and
Dean Karn
committed
Merge pull request #33 from bluesuncorp/v5-development
V5 development
2 parents 829b137 + 765fb71 commit fa8e066

File tree

5 files changed

+67
-27
lines changed

5 files changed

+67
-27
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@ Package validator
44
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/validator.v5?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/validator.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.
7+
It is also capable of Cross Field and Cross Struct validations.
88

99
Installation
1010
============
1111

12-
Just use go get.
12+
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

1818
go get -u gopkg.in/bluesuncorp/validator.v5
1919

20-
And then just import the package into your own code.
20+
Then import the validator package into your own code.
2121

2222
import "gopkg.in/bluesuncorp/validator.v5"
2323

24-
Usage
25-
=====
24+
Usage and documentation
25+
=======================
2626

2727
Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v5 for detailed usage docs.
2828

29-
Contributing
30-
============
29+
How to Contribute
30+
=================
3131

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

35-
If changes are breaking 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: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
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.
2+
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.
33
44
Validate
55
@@ -250,57 +250,65 @@ Here is a list of the current built in validators:
250250
Validating by field validate.FieldWithValue(start, end, "ltefield")
251251
252252
alpha
253-
This validates that a strings value contains alpha characters only
253+
This validates that a string value contains alpha characters only
254254
(Usage: alpha)
255255
256256
alphanum
257-
This validates that a strings value contains alphanumeric characters only
257+
This validates that a string value contains alphanumeric characters only
258258
(Usage: alphanum)
259259
260260
numeric
261-
This validates that a strings value contains a basic numeric value.
261+
This validates that a string value contains a basic numeric value.
262262
basic excludes exponents etc...
263263
(Usage: numeric)
264264
265265
hexadecimal
266-
This validates that a strings value contains a valid hexadecimal.
266+
This validates that a string value contains a valid hexadecimal.
267267
(Usage: hexadecimal)
268268
269269
hexcolor
270-
This validates that a strings value contains a valid hex color including
270+
This validates that a string value contains a valid hex color including
271271
hashtag (#)
272272
(Usage: hexcolor)
273273
274274
rgb
275-
This validates that a strings value contains a valid rgb color
275+
This validates that a string value contains a valid rgb color
276276
(Usage: rgb)
277277
278278
rgba
279-
This validates that a strings value contains a valid rgba color
279+
This validates that a string value contains a valid rgba color
280280
(Usage: rgba)
281281
282282
hsl
283-
This validates that a strings value contains a valid hsl color
283+
This validates that a string value contains a valid hsl color
284284
(Usage: hsl)
285285
286286
hsla
287-
This validates that a strings value contains a valid hsla color
287+
This validates that a string value contains a valid hsla color
288288
(Usage: hsla)
289289
290290
email
291-
This validates that a strings value contains a valid email
291+
This validates that a string value contains a valid email
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
296-
This validates that a strings value contains a valid url
297+
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
301-
This validates that a strings value contains a valid uri
303+
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
@@ -314,7 +322,7 @@ Validator notes:
314322
used within the validator function and even be precompiled for better efficiency
315323
within regexes.go.
316324
317-
And the best reason, you can sumit a pull request and we can keep on adding to the
325+
And the best reason, you can submit a pull request and we can keep on adding to the
318326
validation library of this package!
319327
320328
Panics

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-z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String = "(?:^(?:[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)