Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
. "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
}
Expand Down Expand Up @@ -137,6 +142,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 {
Expand Down