update thrift, opencensus, others (#893)

* update thrift, opencensus, others

* stats: update to opencensus 0.6.0 view api
This commit is contained in:
Reed Allman
2018-03-26 15:43:49 -07:00
committed by GitHub
parent 0ce5c4500b
commit 8af605cf3d
1266 changed files with 122191 additions and 28775 deletions

View File

@@ -0,0 +1,2 @@
<!-- Love govalidator? Please consider supporting our collective:
👉 https://opencollective.com/govalidator/donate -->

View File

@@ -23,4 +23,41 @@ If you don't know what to do, there are some features and functions that need to
Feel free to create what you want, but keep in mind when you implement new features:
- Code must be clear and readable, names of variables/constants clearly describes what they are doing
- Public functions must be documented and described in source file and added to README.md to the list of available functions
- There are must be unit-tests for any new functions and improvements
- There are must be unit-tests for any new functions and improvements
## Financial contributions
We also welcome financial contributions in full transparency on our [open collective](https://opencollective.com/govalidator).
Anyone can file an expense. If the expense makes sense for the development of the community, it will be "merged" in the ledger of our open collective by the core contributors and the person who filed the expense will be reimbursed.
## Credits
### Contributors
Thank you to all the people who have already contributed to govalidator!
<a href="graphs/contributors"><img src="https://opencollective.com/govalidator/contributors.svg?width=890" /></a>
### Backers
Thank you to all our backers! [[Become a backer](https://opencollective.com/govalidator#backer)]
<a href="https://opencollective.com/govalidator#backers" target="_blank"><img src="https://opencollective.com/govalidator/backers.svg?width=890"></a>
### Sponsors
Thank you to all our sponsors! (please ask your company to also support this open source project by [becoming a sponsor](https://opencollective.com/govalidator#sponsor))
<a href="https://opencollective.com/govalidator/sponsor/0/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/0/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/1/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/1/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/2/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/2/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/3/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/3/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/4/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/4/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/5/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/5/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/6/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/6/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/7/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/7/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/8/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/8/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/9/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/9/avatar.svg"></a>

View File

@@ -1,7 +1,7 @@
govalidator
===========
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/asaskevich/govalidator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) [![GoDoc](https://godoc.org/github.com/asaskevich/govalidator?status.png)](https://godoc.org/github.com/asaskevich/govalidator) [![Coverage Status](https://img.shields.io/coveralls/asaskevich/govalidator.svg)](https://coveralls.io/r/asaskevich/govalidator?branch=master) [![wercker status](https://app.wercker.com/status/1ec990b09ea86c910d5f08b0e02c6043/s "wercker status")](https://app.wercker.com/project/bykey/1ec990b09ea86c910d5f08b0e02c6043)
[![Build Status](https://travis-ci.org/asaskevich/govalidator.svg?branch=master)](https://travis-ci.org/asaskevich/govalidator) [![Go Report Card](https://goreportcard.com/badge/github.com/asaskevich/govalidator)](https://goreportcard.com/report/github.com/asaskevich/govalidator) [![GoSearch](http://go-search.org/badge?id=github.com%2Fasaskevich%2Fgovalidator)](http://go-search.org/view?id=github.com%2Fasaskevich%2Fgovalidator)
[![Build Status](https://travis-ci.org/asaskevich/govalidator.svg?branch=master)](https://travis-ci.org/asaskevich/govalidator) [![Go Report Card](https://goreportcard.com/badge/github.com/asaskevich/govalidator)](https://goreportcard.com/report/github.com/asaskevich/govalidator) [![GoSearch](http://go-search.org/badge?id=github.com%2Fasaskevich%2Fgovalidator)](http://go-search.org/view?id=github.com%2Fasaskevich%2Fgovalidator) [![Backers on Open Collective](https://opencollective.com/govalidator/backers/badge.svg)](#backers) [![Sponsors on Open Collective](https://opencollective.com/govalidator/sponsors/badge.svg)](#sponsors)
A package of validators and sanitizers for strings, structs and collections. Based on [validator.js](https://github.com/chriso/validator.js).
@@ -406,6 +406,15 @@ govalidator.CustomTypeTagMap.Set("customMinLengthValidator", CustomTypeValidator
}))
```
###### Custom error messages
Custom error messages are supported via annotations by adding the `~` separator - here's an example of how to use it:
```go
type Ticket struct {
Id int64 `json:"id"`
FirstName string `json:"firstname" valid:"required~First name is blank"`
}
```
#### Notes
Documentation is available here: [godoc.org](https://godoc.org/github.com/asaskevich/govalidator).
Full information about code coverage is also available here: [govalidator on gocover.io](http://gocover.io/github.com/asaskevich/govalidator).
@@ -437,6 +446,11 @@ Feel free to create what you want, but keep in mind when you implement new featu
- Public functions must be documented and described in source file and added to README.md to the list of available functions
- There are must be unit-tests for any new functions and improvements
## Credits
### Contributors
This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
#### Special thanks to [contributors](https://github.com/asaskevich/govalidator/graphs/contributors)
* [Daniel Lohse](https://github.com/annismckenzie)
* [Attila Oláh](https://github.com/attilaolah)
@@ -447,3 +461,30 @@ Feel free to create what you want, but keep in mind when you implement new featu
* [Nathan Davies](https://github.com/nathj07)
* [Matt Sanford](https://github.com/mzsanford)
* [Simon ccl1115](https://github.com/ccl1115)
<a href="graphs/contributors"><img src="https://opencollective.com/govalidator/contributors.svg?width=890" /></a>
### Backers
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/govalidator#backer)]
<a href="https://opencollective.com/govalidator#backers" target="_blank"><img src="https://opencollective.com/govalidator/backers.svg?width=890"></a>
### Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/govalidator#sponsor)]
<a href="https://opencollective.com/govalidator/sponsor/0/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/0/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/1/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/1/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/2/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/2/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/3/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/3/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/4/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/4/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/5/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/5/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/6/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/6/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/7/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/7/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/8/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/8/avatar.svg"></a>
<a href="https://opencollective.com/govalidator/sponsor/9/website" target="_blank"><img src="https://opencollective.com/govalidator/sponsor/9/avatar.svg"></a>

View File

@@ -3,6 +3,7 @@ package govalidator
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
)
@@ -30,13 +31,31 @@ func ToFloat(str string) (float64, error) {
return res, err
}
// ToInt convert the input string to an integer, or 0 if the input is not an integer.
func ToInt(str string) (int64, error) {
res, err := strconv.ParseInt(str, 0, 64)
if err != nil {
// ToInt convert the input string or any int type to an integer type 64, or 0 if the input is not an integer.
func ToInt(value interface{}) (res int64, err error) {
val := reflect.ValueOf(value)
switch value.(type) {
case int, int8, int16, int32, int64:
res = val.Int()
case uint, uint8, uint16, uint32, uint64:
res = int64(val.Uint())
case string:
if IsInt(val.String()) {
res, err = strconv.ParseInt(val.String(), 0, 64)
if err != nil {
res = 0
}
} else {
err = fmt.Errorf("math: square root of negative number %g", value)
res = 0
}
default:
err = fmt.Errorf("math: square root of negative number %g", value)
res = 0
}
return res, err
return
}
// ToBoolean convert the input string to a boolean.

View File

@@ -42,11 +42,14 @@ func IsNonPositive(value float64) bool {
}
// InRange returns true if value lies between left and right border
func InRangeInt(value, left, right int) bool {
if left > right {
left, right = right, left
func InRangeInt(value, left, right interface{}) bool {
value64, _ := ToInt(value)
left64, _ := ToInt(left)
right64, _ := ToInt(right)
if left64 > right64 {
left64, right64 = right64, left64
}
return value >= left && value <= right
return value64 >= left64 && value64 <= right64
}
// InRange returns true if value lies between left and right border

View File

@@ -181,7 +181,7 @@ func TestIsNatural(t *testing.T) {
func TestInRangeInt(t *testing.T) {
t.Parallel()
var tests = []struct {
var testAsInts = []struct {
param int
left int
right int
@@ -196,10 +196,210 @@ func TestInRangeInt(t *testing.T) {
{0, 0, -1, true},
{0, 10, 5, false},
}
for _, test := range tests {
for _, test := range testAsInts {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v", test.param, test.left, test.right, test.expected, actual)
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsInt8s = []struct {
param int8
left int8
right int8
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{-1, 0, 0, false},
{0, -1, 1, true},
{0, 0, 1, true},
{0, -1, 0, true},
{0, 0, -1, true},
{0, 10, 5, false},
}
for _, test := range testAsInt8s {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int8", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsInt16s = []struct {
param int16
left int16
right int16
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{-1, 0, 0, false},
{0, -1, 1, true},
{0, 0, 1, true},
{0, -1, 0, true},
{0, 0, -1, true},
{0, 10, 5, false},
}
for _, test := range testAsInt16s {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int16", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsInt32s = []struct {
param int32
left int32
right int32
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{-1, 0, 0, false},
{0, -1, 1, true},
{0, 0, 1, true},
{0, -1, 0, true},
{0, 0, -1, true},
{0, 10, 5, false},
}
for _, test := range testAsInt32s {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int32", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsInt64s = []struct {
param int64
left int64
right int64
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{-1, 0, 0, false},
{0, -1, 1, true},
{0, 0, 1, true},
{0, -1, 0, true},
{0, 0, -1, true},
{0, 10, 5, false},
}
for _, test := range testAsInt64s {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type int64", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsUInts = []struct {
param uint
left uint
right uint
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{0, 0, 1, true},
{0, 10, 5, false},
}
for _, test := range testAsUInts {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsUInt8s = []struct {
param uint8
left uint8
right uint8
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{0, 0, 1, true},
{0, 10, 5, false},
}
for _, test := range testAsUInt8s {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsUInt16s = []struct {
param uint16
left uint16
right uint16
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{0, 0, 1, true},
{0, 10, 5, false},
}
for _, test := range testAsUInt16s {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsUInt32s = []struct {
param uint32
left uint32
right uint32
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{0, 0, 1, true},
{0, 10, 5, false},
}
for _, test := range testAsUInt32s {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsUInt64s = []struct {
param uint64
left uint64
right uint64
expected bool
}{
{0, 0, 0, true},
{1, 0, 0, false},
{0, 0, 1, true},
{0, 10, 5, false},
}
for _, test := range testAsUInt64s {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type uint", test.param, test.left, test.right, test.expected, actual)
}
}
var testAsStrings = []struct {
param string
left string
right string
expected bool
}{
{"0", "0", "0", true},
{"1", "0", "0", false},
{"-1", "0", "0", false},
{"0", "-1", "1", true},
{"0", "0", "1", true},
{"0", "-1", "0", true},
{"0", "0", "-1", true},
{"0", "10", "5", false},
}
for _, test := range testAsStrings {
actual := InRangeInt(test.param, test.left, test.right)
if actual != test.expected {
t.Errorf("Expected InRangeInt(%v, %v, %v) to be %v, got %v using type string", test.param, test.left, test.right, test.expected, actual)
}
}
}

View File

@@ -4,7 +4,7 @@ import "regexp"
// Basic regular expressions for validating strings
const (
Email string = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(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{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
CreditCard string = "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$"
ISBN10 string = "^(?:[0-9]{9}X|[0-9]{10})$"
ISBN13 string = "^(?:[0-9]{13})$"
@@ -43,6 +43,8 @@ const (
UnixPath string = `^(/[^/\x00]*)+/?$`
Semver string = "^v?(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(-(0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(\\.(0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\\+[0-9a-zA-Z-]+(\\.[0-9a-zA-Z-]+)*)?$"
tagName string = "valid"
hasLowerCase string = ".*[[:lower:]]"
hasUpperCase string = ".*[[:upper:]]"
)
// Used by IsFilePath func
@@ -56,7 +58,10 @@ const (
)
var (
rxEmail = regexp.MustCompile(Email)
userRegexp = regexp.MustCompile("^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$")
hostRegexp = regexp.MustCompile("^[^\\s]+\\.[^\\s]+$")
userDotRegexp = regexp.MustCompile("(^[.]{1})|([.]{1}$)|([.]{2,})")
//rxEmail = regexp.MustCompile(Email)
rxCreditCard = regexp.MustCompile(CreditCard)
rxISBN10 = regexp.MustCompile(ISBN10)
rxISBN13 = regexp.MustCompile(ISBN13)
@@ -87,4 +92,6 @@ var (
rxWinPath = regexp.MustCompile(WinPath)
rxUnixPath = regexp.MustCompile(UnixPath)
rxSemver = regexp.MustCompile(Semver)
rxHasLowerCase = regexp.MustCompile(hasLowerCase)
rxHasUpperCase = regexp.MustCompile(hasUpperCase)
)

View File

@@ -108,7 +108,9 @@ func CamelCaseToUnderscore(str string) string {
var output []rune
var segment []rune
for _, r := range str {
if !unicode.IsLower(r) && string(r) != "_" {
// not treat number as separate segment
if !unicode.IsLower(r) && string(r) != "_" && !unicode.IsNumber(r) {
output = addSegment(output, segment)
segment = nil
}

View File

@@ -0,0 +1,17 @@
package govalidator
import "testing"
func BenchmarkContains(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
Contains("a0b01c012deffghijklmnopqrstu0123456vwxyz", "0123456789")
}
}
func BenchmarkMatches(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
Matches("alfkjl12309fdjldfsa209jlksdfjLAKJjs9uJH234", "[\\w\\d]+")
}
}

View File

@@ -270,6 +270,7 @@ func TestCamelCaseToUnderscore(t *testing.T) {
{"ABC", "a_b_c"},
{"1B", "1_b"},
{"foo_bar", "foo_bar"},
{"FooV2Bar", "foo_v2_bar"},
}
for _, test := range tests {
actual := CamelCaseToUnderscore(test.param)
@@ -395,7 +396,7 @@ func TestNormalizeEmail(t *testing.T) {
{`some.name.midd.lena.me.+extension@gmail.com`, `somenamemiddlename@gmail.com`},
{`some.name.midd.lena.me.+extension@googlemail.com`, `somenamemiddlename@gmail.com`},
{`some.name+extension@unknown.com`, `some.name+extension@unknown.com`},
{`hans@m端ller.com`, `hans@m端ller.com`},
// TODO: {`hans@m端ller.com`, `hans@m端ller.com`},
{`hans`, ``},
}
for _, test := range tests {

View File

@@ -52,9 +52,33 @@ func SetFieldsRequiredByDefault(value bool) {
}
// IsEmail check if the string is an email.
func IsEmail(str string) bool {
// TODO uppercase letters are not supported
return rxEmail.MatchString(str)
func IsEmail(email string) bool {
if len(email) < 6 || len(email) > 254 {
return false
}
at := strings.LastIndex(email, "@")
if at <= 0 || at > len(email)-3 {
return false
}
user := email[:at]
host := email[at+1:]
if len(user) > 64 {
return false
}
if userDotRegexp.MatchString(user) || !userRegexp.MatchString(user) || !hostRegexp.MatchString(host) {
return false
}
switch host {
case "localhost", "example.com":
return true
}
if _, err := net.LookupMX(host); err != nil {
if _, err := net.LookupIP(host); err != nil {
return false
}
}
return true
}
// IsURL check if the string is an URL.
@@ -231,6 +255,22 @@ func IsUpperCase(str string) bool {
return str == strings.ToUpper(str)
}
// HasLowerCase check if the string contains at least 1 lowercase. Empty string is valid.
func HasLowerCase(str string) bool {
if IsNull(str) {
return true
}
return rxHasLowerCase.MatchString(str)
}
// HasUpperCase check if the string contians as least 1 uppercase. Empty string is valid.
func HasUpperCase(str string) bool {
if IsNull(str) {
return true
}
return rxHasUpperCase.MatchString(str)
}
// IsInt check if the string is an integer. Empty string is valid.
func IsInt(str string) bool {
if IsNull(str) {
@@ -523,7 +563,7 @@ func IsHash(str string, algorithm string) bool {
return false
}
return Matches(str, "^[a-f0-9]{" + len + "}$")
return Matches(str, "^[a-f0-9]{"+len+"}$")
}
// IsDialString validates the given string for usage with the various Dial() functions
@@ -678,7 +718,9 @@ func ValidateStruct(s interface{}) (bool, error) {
continue // Private field
}
structResult := true
if valueField.Kind() == reflect.Struct && typeField.Tag.Get(tagName) != "-" {
if (valueField.Kind() == reflect.Struct ||
(valueField.Kind() == reflect.Ptr && valueField.Elem().Kind() == reflect.Struct)) &&
typeField.Tag.Get(tagName) != "-" {
var err error
structResult, err = ValidateStruct(valueField.Interface())
if err != nil {
@@ -890,7 +932,7 @@ func checkRequired(v reflect.Value, t reflect.StructField, options tagOptionsMap
}
return false, Error{t.Name, fmt.Errorf("non zero value required"), false, "required"}
} else if _, isOptional := options["optional"]; fieldsRequiredByDefault && !isOptional {
return false, Error{t.Name, fmt.Errorf("All fields are required to at least have one validation defined"), false, "required"}
return false, Error{t.Name, fmt.Errorf("Missing required field"), false, "required"}
}
// not required and empty is valid
return true, nil
@@ -994,7 +1036,11 @@ func typeCheck(v reflect.Value, t reflect.StructField, o reflect.Value, options
delete(options, validatorSpec)
switch v.Kind() {
case reflect.String:
case reflect.String,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
field := fmt.Sprint(v) // make value into string, then validate with regex
if result := validatefunc(field, ps[1:]...); (!result && !negate) || (result && negate) {
if customMsgExists {

View File

@@ -499,6 +499,74 @@ func TestIsUpperCase(t *testing.T) {
}
}
func TestHasLowerCase(t *testing.T) {
t.Parallel()
var tests = []struct {
param string
expected bool
}{
{"", true},
{"abc123", true},
{"abc", true},
{"a b c", true},
{"abcß", true},
{"abcẞ", true},
{"ABCẞ", false},
{"tr竪s 端ber", true},
{"fooBar", true},
{"123ABC", false},
{"ABC123", false},
{"ABC", false},
{"S T R", false},
{"fooBar", true},
{"abacaba123", true},
{"FÒÔBÀŘ", false},
{"fòôbàř", true},
{"fÒÔBÀŘ", true},
}
for _, test := range tests {
actual := HasLowerCase(test.param)
if actual != test.expected {
t.Errorf("Expected HasLowerCase(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}
func TestHasUpperCase(t *testing.T) {
t.Parallel()
var tests = []struct {
param string
expected bool
}{
{"", true},
{"abc123", false},
{"abc", false},
{"a b c", false},
{"abcß", false},
{"abcẞ", false},
{"ABCẞ", true},
{"tr竪s 端ber", false},
{"fooBar", true},
{"123ABC", true},
{"ABC123", true},
{"ABC", true},
{"S T R", true},
{"fooBar", true},
{"abacaba123", false},
{"FÒÔBÀŘ", true},
{"fòôbàř", false},
{"Fòôbàř", true},
}
for _, test := range tests {
actual := HasUpperCase(test.param)
if actual != test.expected {
t.Errorf("Expected HasUpperCase(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}
func TestIsInt(t *testing.T) {
t.Parallel()
@@ -536,13 +604,12 @@ func TestIsInt(t *testing.T) {
}
}
func TestIsHash(t *testing.T) {
t.Parallel()
var tests = []struct {
param string
algo string
algo string
expected bool
}{
{"3ca25ae354e192b26879f651a51d92aa8a34d8d3", "sha1", true},
@@ -576,19 +643,13 @@ func TestIsEmail(t *testing.T) {
}{
{"", false},
{"foo@bar.com", true},
{"x@x.x", true},
{"foo@bar.com.au", true},
{"foo+bar@bar.com", true},
{"foo@bar.coffee", true},
{"foo@bar.coffee..coffee", false},
{"foo@bar.bar.coffee", true},
{"foo@bar.中文网", true},
{"invalidemail@", false},
{"invalid.com", false},
{"@invalid.com", false},
{"test|123@m端ller.com", true},
{"hans@m端ller.com", true},
{"hans.m端ller@test.com", true},
{"NathAn.daVIeS@DomaIn.cOM", true},
{"NATHAN.DAVIES@DOMAIN.CO.UK", true},
}
@@ -744,7 +805,6 @@ func TestIsRequestURL(t *testing.T) {
{"http://www.foo---bar.com/", true},
{"mailto:someone@example.com", true},
{"irc://irc.server.org/channel", true},
{"irc://#channel@network", true},
{"/abs/test/dir", false},
{"./rel/test/dir", false},
}
@@ -793,7 +853,6 @@ func TestIsRequestURI(t *testing.T) {
{"http://www.foo---bar.com/", true},
{"mailto:someone@example.com", true},
{"irc://irc.server.org/channel", true},
{"irc://#channel@network", true},
{"/abs/test/dir", true},
{"./rel/test/dir", false},
}
@@ -2116,6 +2175,15 @@ type MissingValidationDeclarationStruct struct {
Email string `valid:"required,email"`
}
type FieldRequiredByDefault struct {
Email string `valid:"email"`
}
type MultipleFieldsRequiredByDefault struct {
Url string `valid:"url"`
Email string `valid:"email"`
}
type FieldsRequiredByDefaultButExemptStruct struct {
Name string `valid:"-"`
Email string `valid:"email"`
@@ -2152,6 +2220,46 @@ func TestValidateMissingValidationDeclarationStruct(t *testing.T) {
SetFieldsRequiredByDefault(false)
}
func TestFieldRequiredByDefault(t *testing.T) {
var tests = []struct {
param FieldRequiredByDefault
expected bool
}{
{FieldRequiredByDefault{}, false},
}
SetFieldsRequiredByDefault(true)
for _, test := range tests {
actual, err := ValidateStruct(test.param)
if actual != test.expected {
t.Errorf("Expected ValidateStruct(%q) to be %v, got %v", test.param, test.expected, actual)
if err != nil {
t.Errorf("Got Error on ValidateStruct(%q): %s", test.param, err)
}
}
}
SetFieldsRequiredByDefault(false)
}
func TestMultipleFieldsRequiredByDefault(t *testing.T) {
var tests = []struct {
param MultipleFieldsRequiredByDefault
expected bool
}{
{MultipleFieldsRequiredByDefault{}, false},
}
SetFieldsRequiredByDefault(true)
for _, test := range tests {
actual, err := ValidateStruct(test.param)
if actual != test.expected {
t.Errorf("Expected ValidateStruct(%q) to be %v, got %v", test.param, test.expected, actual)
if err != nil {
t.Errorf("Got Error on ValidateStruct(%q): %s", test.param, err)
}
}
}
SetFieldsRequiredByDefault(false)
}
func TestFieldsRequiredByDefaultButExemptStruct(t *testing.T) {
var tests = []struct {
param FieldsRequiredByDefaultButExemptStruct
@@ -2535,6 +2643,7 @@ func TestValidateStruct(t *testing.T) {
{User{"John", "", "12345", 0, &Address{"Street", "123456789"}, []Address{{"Street", "ABC456D89"}, {"Street", "123456"}}}, false},
{UserValid{"John", "john@yahoo.com", "123G#678", 20, &Address{"Street", "123456"}, []Address{{"Street", "123456"}, {"Street", "123456"}}}, true},
{UserValid{"John", "john!yahoo.com", "12345678", 20, &Address{"Street", "ABC456D89"}, []Address{}}, false},
{UserValid{"John", "john@yahoo.com", "12345678", 20, &Address{"Street", "123456xxx"}, []Address{{"Street", "123456"}, {"Street", "123456"}}}, false},
{UserValid{"John", "john!yahoo.com", "12345678", 20, &Address{"Street", "ABC456D89"}, []Address{{"Street", "ABC456D89"}, {"Street", "123456"}}}, false},
{UserValid{"John", "", "12345", 0, &Address{"Street", "123456789"}, []Address{{"Street", "ABC456D89"}, {"Street", "123456"}}}, false},
{nil, true},
@@ -2852,6 +2961,73 @@ func ExampleValidateStruct() {
println(result)
}
func TestValidateStructParamValidatorInt(t *testing.T) {
type Test1 struct {
Int int `valid:"range(1|10)"`
Int8 int8 `valid:"range(1|10)"`
Int16 int16 `valid:"range(1|10)"`
Int32 int32 `valid:"range(1|10)"`
Int64 int64 `valid:"range(1|10)"`
Uint uint `valid:"range(1|10)"`
Uint8 uint8 `valid:"range(1|10)"`
Uint16 uint16 `valid:"range(1|10)"`
Uint32 uint32 `valid:"range(1|10)"`
Uint64 uint64 `valid:"range(1|10)"`
Float32 float32 `valid:"range(1|10)"`
Float64 float64 `valid:"range(1|10)"`
}
test1Ok := &Test1{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}
test1NotOk := &Test1{11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}
_, err := ValidateStruct(test1Ok)
if err != nil {
t.Errorf("Test failed: %s", err)
}
_, err = ValidateStruct(test1NotOk)
if err == nil {
t.Errorf("Test failed: nil")
}
type Test2 struct {
Int int `valid:"in(1|10)"`
Int8 int8 `valid:"in(1|10)"`
Int16 int16 `valid:"in(1|10)"`
Int32 int32 `valid:"in(1|10)"`
Int64 int64 `valid:"in(1|10)"`
Uint uint `valid:"in(1|10)"`
Uint8 uint8 `valid:"in(1|10)"`
Uint16 uint16 `valid:"in(1|10)"`
Uint32 uint32 `valid:"in(1|10)"`
Uint64 uint64 `valid:"in(1|10)"`
Float32 float32 `valid:"in(1|10)"`
Float64 float64 `valid:"in(1|10)"`
}
test2Ok1 := &Test2{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
test2Ok2 := &Test2{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
test2NotOk := &Test2{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
_, err = ValidateStruct(test2Ok1)
if err != nil {
t.Errorf("Test failed: %s", err)
}
_, err = ValidateStruct(test2Ok2)
if err != nil {
t.Errorf("Test failed: %s", err)
}
_, err = ValidateStruct(test2NotOk)
if err == nil {
t.Errorf("Test failed: nil")
}
}
func TestIsCIDR(t *testing.T) {
t.Parallel()