Merge pull request #32 from joeybloggs/v5-development

V5 development
pull/33/head
Dean Karn 10 years ago
commit 765fb71956
  1. 10
      README.md
  2. 5
      baked_in.go
  3. 8
      doc.go
  4. 2
      regexes.go
  5. 25
      validator_test.go

@ -11,7 +11,7 @@ Installation
Use go get. Use go get.
go get gopkg.in/bluesuncorp/validator.v5 go get -u gopkg.in/bluesuncorp/validator.v5
or to update or to update
@ -29,12 +29,12 @@ Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v5 for detailed usage
How to Contribute 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. 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 If the changes being proposed or requested are breaking changes, please create an issue, for discussion
the highest development branch for example this package has a v1 and v1-development branch or create a pull request against the highest development branch for example this package has a
however, there will also be a v2-development brach even though v2 doesn't exist yet. 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 I strongly encourage everyone whom creates a custom validation function to contribute them and
help make this package even better. help make this package even better.

@ -37,6 +37,11 @@ var BakedInValidators = map[string]Func{
"email": isEmail, "email": isEmail,
"url": isURL, "url": isURL,
"uri": isURI, "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 { func isURI(top interface{}, current interface{}, field interface{}, param string) bool {

@ -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 This may not conform to all possibilities of any rfc standard, but neither
does any email provider accept all posibilities... does any email provider accept all posibilities...
(Usage: email) (Usage: email)
url url
This validates that a string 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 This will accept any url the golang request uri accepts but must contain
a schema for example http:// or rtmp:// a schema for example http:// or rtmp://
(Usage: url) (Usage: url)
uri uri
This validates that a string 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) 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: Validator notes:
regex regex

@ -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*\\)$" 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*\\)$" 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\\x0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0-9+\\/]{3}=)$)"
) )
var ( var (
@ -28,6 +29,7 @@ var (
hslRegex = regexp.MustCompile(hslRegexString) hslRegex = regexp.MustCompile(hslRegexString)
hslaRegex = regexp.MustCompile(hslaRegexString) hslaRegex = regexp.MustCompile(hslaRegexString)
emailRegex = regexp.MustCompile(emailRegexString) emailRegex = regexp.MustCompile(emailRegexString)
base64Regex = regexp.MustCompile(base64RegexString)
) )
func matchesRegex(regex *regexp.Regexp, field interface{}) bool { func matchesRegex(regex *regexp.Regexp, field interface{}) bool {

@ -10,6 +10,11 @@ import (
. "gopkg.in/check.v1" . "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 { type I interface {
Foo() string Foo() string
} }
@ -137,6 +142,26 @@ func isEqualFunc(val interface{}, current interface{}, field interface{}, param
return current.(string) == field.(string) 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) { func (ms *MySuite) TestStructOnlyValidation(c *C) {
type Inner struct { type Inner struct {

Loading…
Cancel
Save