Merge pull request #34 from bluesuncorp/v5

merge latest changes from v5 into v6-development
pull/58/head
Dean Karn 10 years ago
commit 98f3221631
  1. 28
      README.md
  2. 5
      baked_in.go
  3. 36
      doc.go
  4. 2
      regexes.go
  5. 25
      validator_test.go

@ -1,40 +1,40 @@
Package validator
================
[![Build Status](https://travis-ci.org/bluesuncorp/validator.svg?branch=v5)](https://travis-ci.org/bluesuncorp/validator)
[![Build Status](https://travis-ci.org/bluesuncorp/validator.svg?branch=v5.0.2)](https://travis-ci.org/bluesuncorp/validator)
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/validator.v5?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/validator.v5)
Package validator implements value validations for structs and individual fields based on tags.
It is even capable of Cross Field and even Cross Field Cross Struct validation.
It is also capable of Cross Field and Cross Struct validations.
Installation
============
Just use go get.
Use go get.
go get gopkg.in/bluesuncorp/validator.v5
go get -u gopkg.in/bluesuncorp/validator.v5
or to update
go get -u gopkg.in/bluesuncorp/validator.v5
And then just import the package into your own code.
Then import the validator package into your own code.
import "gopkg.in/bluesuncorp/validator.v5"
Usage
=====
Usage and documentation
=======================
Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v5 for detailed usage docs.
Contributing
============
How to Contribute
=================
There will be a development branch for each version of this package i.e. v1-development, please
make your pull requests against those branches.
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 are breaking 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.

@ -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 {

@ -1,5 +1,5 @@
/*
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.
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.
Validate
@ -250,57 +250,65 @@ Here is a list of the current built in validators:
Validating by field validate.FieldWithValue(start, end, "ltefield")
alpha
This validates that a strings value contains alpha characters only
This validates that a string value contains alpha characters only
(Usage: alpha)
alphanum
This validates that a strings value contains alphanumeric characters only
This validates that a string value contains alphanumeric characters only
(Usage: alphanum)
numeric
This validates that a strings value contains a basic numeric value.
This validates that a string value contains a basic numeric value.
basic excludes exponents etc...
(Usage: numeric)
hexadecimal
This validates that a strings value contains a valid hexadecimal.
This validates that a string value contains a valid hexadecimal.
(Usage: hexadecimal)
hexcolor
This validates that a strings value contains a valid hex color including
This validates that a string value contains a valid hex color including
hashtag (#)
(Usage: hexcolor)
rgb
This validates that a strings value contains a valid rgb color
This validates that a string value contains a valid rgb color
(Usage: rgb)
rgba
This validates that a strings value contains a valid rgba color
This validates that a string value contains a valid rgba color
(Usage: rgba)
hsl
This validates that a strings value contains a valid hsl color
This validates that a string value contains a valid hsl color
(Usage: hsl)
hsla
This validates that a strings value contains a valid hsla color
This validates that a string value contains a valid hsla color
(Usage: hsla)
email
This validates that a strings value contains a valid email
This validates that a string value contains a valid email
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 strings 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
a schema for example http:// or rtmp://
(Usage: url)
uri
This validates that a strings 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)
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
@ -314,7 +322,7 @@ Validator notes:
used within the validator function and even be precompiled for better efficiency
within regexes.go.
And the best reason, you can sumit a pull request and we can keep on adding to the
And the best reason, you can submit a pull request and we can keep on adding to the
validation library of this package!
Panics

@ -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 {

@ -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
}
@ -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 {

Loading…
Cancel
Save