Skip to content

Commit ae8ecbc

Browse files
author
Dean Karn
authored
Merge branch 'master' into master
2 parents b15bf1d + 0ab8e03 commit ae8ecbc

File tree

11 files changed

+2259
-22
lines changed

11 files changed

+2259
-22
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: go
22
go:
3-
- 1.13.4
3+
- 1.13.7
44
- tip
55
matrix:
66
allow_failures:
@@ -25,5 +25,5 @@ script:
2525
- go test -v -race -covermode=atomic -coverprofile=coverage.coverprofile ./...
2626

2727
after_success: |
28-
[ $TRAVIS_GO_VERSION = 1.13.4 ] &&
28+
[ $TRAVIS_GO_VERSION = 1.13.7 ] &&
2929
goveralls -coverprofile=coverage.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN

README.md

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
Package validator
22
================
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">[![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4-
![Project status](https://img.shields.io/badge/version-10.1.0-green.svg)
4+
![Project status](https://img.shields.io/badge/version-10.2.0-green.svg)
55
[![Build Status](https://travis-ci.org/go-playground/validator.svg?branch=master)](https://travis-ci.org/go-playground/validator)
66
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-playground/validator?branch=master)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)
8-
[![GoDoc](https://godoc.org/github.com/go-playground/validator?status.svg)](https://godoc.org/github.com/go-playground/validator)
8+
[![GoDoc](https://godoc.org/github.com/go-playground/validator?status.svg)](https://pkg.go.dev/github.com/go-playground/validator/v10)
99
![License](https://img.shields.io/dub/l/vibe-d.svg)
1010

1111
Package validator implements value validations for structs and individual fields based on tags.
1212

1313
It has the following **unique** features:
1414

15-
- Cross Field and Cross Struct validations by using validation tags or custom validators.
15+
- Cross Field and Cross Struct validations by using validation tags or custom validators.
1616
- Slice, Array and Map diving, which allows any or all levels of a multidimensional field to be validated.
17-
- Ability to dive into both map keys and values for validation
17+
- Ability to dive into both map keys and values for validation
1818
- Handles type interface by determining it's underlying type prior to validation.
1919
- Handles custom field types such as sql driver Valuer see [Valuer](https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29)
2020
- Alias validation tags, which allows for mapping of several validations to a single tag for easier defining of validations on structs
@@ -64,6 +64,145 @@ Please see https://godoc.org/github.com/go-playground/validator for detailed usa
6464
- [Gin upgrade and/or override validator](https://github.com/go-playground/validator/tree/v9/_examples/gin-upgrading-overriding)
6565
- [wash - an example application putting it all together](https://github.com/bluesuncorp/wash)
6666

67+
Baked-in Validations
68+
------
69+
70+
### Fields:
71+
72+
| Tag | Description |
73+
| - | - |
74+
| eqcsfield | Field Equals Another Field (relative)|
75+
| eqfield | Field Equals Another Field |
76+
| fieldcontains | NOT DOCUMENTED IN doc.go |
77+
| fieldexcludes | NOT DOCUMENTED IN doc.go |
78+
| gtcsfield | Field Greater Than Another Relative Field |
79+
| gtecsfield | Field Greater Than or Equal To Another Relative Field |
80+
| gtefield | Field Greater Than or Equal To Another Field |
81+
| gtfield | Field Greater Than Another Field |
82+
| ltcsfield | Less Than Another Relative Field |
83+
| ltecsfield | Less Than or Equal To Another Relative Field |
84+
| ltefield | Less Than or Equal To Another Field |
85+
| ltfield | Less Than Another Field |
86+
| necsfield | Field Does Not Equal Another Field (relative) |
87+
| nefield | Field Does Not Equal Another Field |
88+
89+
### Network:
90+
91+
| Tag | Description |
92+
| - | - |
93+
| cidr | Classless Inter-Domain Routing CIDR |
94+
| cidrv4 | Classless Inter-Domain Routing CIDRv4 |
95+
| cidrv6 | Classless Inter-Domain Routing CIDRv6 |
96+
| datauri | Data URL |
97+
| fqdn | Full Qualified Domain Name (FQDN) |
98+
| hostname | Hostname RFC 952 |
99+
| hostname_port | HostPort |
100+
| hostname_rfc1123 | Hostname RFC 1123 |
101+
| ip | Internet Protocol Address IP |
102+
| ip4_addr | Internet Protocol Address IPv4 |
103+
| ip6_addr |Internet Protocol Address IPv6 |
104+
| ip_addr | Internet Protocol Address IP |
105+
| ipv4 | Internet Protocol Address IPv4 |
106+
| ipv6 | Internet Protocol Address IPv6 |
107+
| mac | Media Access Control Address MAC |
108+
| tcp4_addr | Transmission Control Protocol Address TCPv4 |
109+
| tcp6_addr | Transmission Control Protocol Address TCPv6 |
110+
| tcp_addr | Transmission Control Protocol Address TCP |
111+
| udp4_addr | User Datagram Protocol Address UDPv4 |
112+
| udp6_addr | User Datagram Protocol Address UDPv6 |
113+
| udp_addr | User Datagram Protocol Address UDP |
114+
| unix_addr | Unix domain socket end point Address |
115+
| uri | URI String |
116+
| url | URL String |
117+
| url_encoded | URL Encoded |
118+
| urn_rfc2141 | Urn RFC 2141 String |
119+
120+
### Strings:
121+
122+
| Tag | Description |
123+
| - | - |
124+
| alpha | Alpha Only |
125+
| alphanum | Alphanumeric |
126+
| alphanumunicode | Alphanumeric Unicode |
127+
| alphaunicode | Alpha Unicode |
128+
| ascii | ASCII |
129+
| contains | Contains |
130+
| containsany | Contains Any |
131+
| containsrune | Contains Rune |
132+
| lowercase | Lowercase |
133+
| multibyte | Multi-Byte Characters |
134+
| number | NOT DOCUMENTED IN doc.go |
135+
| numeric | Numeric |
136+
| printascii | Printable ASCII |
137+
| startswith | Starts With |
138+
| uppercase | Uppercase |
139+
140+
### Format:
141+
| Tag | Description |
142+
| - | - |
143+
| base64 | Base64 String |
144+
| base64url | Base64URL String |
145+
| btc_addr | Bitcoin Address |
146+
| btc_addr_bech32 | Bitcoin Bech32 Address (segwit) |
147+
| datetime | Datetime |
148+
| email | E-mail String
149+
| eth_addr | Ethereum Address |
150+
| hexadecimal | Hexadecimal String |
151+
| hexcolor | Hexcolor String |
152+
| hsl | HSL String |
153+
| hsla | HSLA String |
154+
| html | HTML Tags |
155+
| html_encoded | HTML Encoded |
156+
| isbn | International Standard Book Number |
157+
| isbn10 | International Standard Book Number 10 |
158+
| isbn13 | International Standard Book Number 13 |
159+
| json | JSON |
160+
| latitude | Latitude |
161+
| longitude | Longitude |
162+
| rgb | RGB String |
163+
| rgba | RGBA String |
164+
| ssn | Social Security Number SSN |
165+
| uuid | Universally Unique Identifier UUID |
166+
| uuid3 | Universally Unique Identifier UUID v3 |
167+
| uuid3_rfc4122 | Universally Unique Identifier UUID v3 RFC4122 |
168+
| uuid4 | Universally Unique Identifier UUID v4 |
169+
| uuid4_rfc4122 | Universally Unique Identifier UUID v4 RFC4122 |
170+
| uuid5 | Universally Unique Identifier UUID v5 |
171+
| uuid5_rfc4122 | Universally Unique Identifier UUID v5 RFC4122 |
172+
| uuid_rfc4122 | Universally Unique Identifier UUID RFC4122 |
173+
174+
### Comparisons:
175+
| Tag | Description |
176+
| - | - |
177+
| eq | Equals |
178+
| gt | Greater than|
179+
| gte |Greater than or equal |
180+
| lt | Less Than |
181+
| lte | Less Than or Equal |
182+
| ne | Not Equal |
183+
184+
### Other:
185+
| Tag | Description |
186+
| - | - |
187+
| dir | Directory |
188+
| e164 | NOT DOCUMENTED IN doc.go |
189+
| endswith | Ends With |
190+
| excludes | Excludes |
191+
| excludesall | Excludes All |
192+
| excludesrune | Excludes Rune |
193+
| file | File path |
194+
| isdefault | Is Default |
195+
| len | Length |
196+
| max | Maximum |
197+
| min | Minimum |
198+
| oneof | One Of |
199+
| required | Required |
200+
| required_with | Required With |
201+
| required_with_all | Required With All |
202+
| required_without | Required Without |
203+
| required_without_all | Required Without All |
204+
| unique | Unique |
205+
67206
Benchmarks
68207
------
69208
###### Run on MacBook Pro (15-inch, 2017) go version go1.10.2 darwin/amd64

baked_in.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ var (
120120
"excludesrune": excludesRune,
121121
"startswith": startsWith,
122122
"endswith": endsWith,
123+
"startsnotwith": startsNotWith,
124+
"endsnotwith": endsNotWith,
123125
"isbn": isISBN,
124126
"isbn10": isISBN10,
125127
"isbn13": isISBN13,
@@ -690,6 +692,16 @@ func endsWith(fl FieldLevel) bool {
690692
return strings.HasSuffix(fl.Field().String(), fl.Param())
691693
}
692694

695+
// StartsNotWith is the validation function for validating that the field's value does not start with the text specified within the param.
696+
func startsNotWith(fl FieldLevel) bool {
697+
return !startsWith(fl)
698+
}
699+
700+
// EndsNotWith is the validation function for validating that the field's value does not end with the text specified within the param.
701+
func endsNotWith(fl FieldLevel) bool {
702+
return !endsWith(fl)
703+
}
704+
693705
// FieldContains is the validation function for validating if the current field's value contains the field specified by the param's value.
694706
func fieldContains(fl FieldLevel) bool {
695707
field := fl.Field()

doc.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,13 @@ This validates that a string value contains unicode alphanumeric characters only
620620
621621
Usage: alphanumunicode
622622
623+
Number
624+
625+
This validates that a string value contains number values only.
626+
For integers or float it returns true.
627+
628+
Usage: number
629+
623630
Numeric
624631
625632
This validates that a string value contains a basic numeric value.
@@ -814,6 +821,18 @@ This validates that a string value ends with the supplied string value
814821
815822
Usage: endswith=goodbye
816823
824+
Does Not Start With
825+
826+
This validates that a string value does not start with the supplied string value
827+
828+
Usage: startsnotwith=hello
829+
830+
Does Not End With
831+
832+
This validates that a string value does not end with the supplied string value
833+
834+
Usage: endsnotwith=goodbye
835+
817836
International Standard Book Number
818837
819838
This validates that a string value contains a valid isbn10 or isbn13 value.

errors.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type FieldError interface {
9999
ActualTag() string
100100

101101
// returns the namespace for the field error, with the tag
102-
// name taking precedence over the fields actual name.
102+
// name taking precedence over the field's actual name.
103103
//
104104
// eg. JSON name "User.fname"
105105
//
@@ -109,29 +109,29 @@ type FieldError interface {
109109
// using validate.Field(...) as there is no way to extract it's name
110110
Namespace() string
111111

112-
// returns the namespace for the field error, with the fields
112+
// returns the namespace for the field error, with the field's
113113
// actual name.
114114
//
115115
// eq. "User.FirstName" see Namespace for comparison
116116
//
117117
// NOTE: this field can be blank when validating a single primitive field
118-
// using validate.Field(...) as there is no way to extract it's name
118+
// using validate.Field(...) as there is no way to extract its name
119119
StructNamespace() string
120120

121121
// returns the fields name with the tag name taking precedence over the
122-
// fields actual name.
122+
// field's actual name.
123123
//
124124
// eq. JSON name "fname"
125125
// see StructField for comparison
126126
Field() string
127127

128-
// returns the fields actual name from the struct, when able to determine.
128+
// returns the field's actual name from the struct, when able to determine.
129129
//
130130
// eq. "FirstName"
131131
// see Field for comparison
132132
StructField() string
133133

134-
// returns the actual fields value in case needed for creating the error
134+
// returns the actual field's value in case needed for creating the error
135135
// message
136136
Value() interface{}
137137

@@ -190,19 +190,19 @@ func (fe *fieldError) ActualTag() string {
190190
}
191191

192192
// Namespace returns the namespace for the field error, with the tag
193-
// name taking precedence over the fields actual name.
193+
// name taking precedence over the field's actual name.
194194
func (fe *fieldError) Namespace() string {
195195
return fe.ns
196196
}
197197

198-
// StructNamespace returns the namespace for the field error, with the fields
198+
// StructNamespace returns the namespace for the field error, with the field's
199199
// actual name.
200200
func (fe *fieldError) StructNamespace() string {
201201
return fe.structNs
202202
}
203203

204-
// Field returns the fields name with the tag name taking precedence over the
205-
// fields actual name.
204+
// Field returns the field's name with the tag name taking precedence over the
205+
// field's actual name.
206206
func (fe *fieldError) Field() string {
207207

208208
return fe.ns[len(fe.ns)-int(fe.fieldLen):]
@@ -218,13 +218,13 @@ func (fe *fieldError) Field() string {
218218
// return fld
219219
}
220220

221-
// returns the fields actual name from the struct, when able to determine.
221+
// returns the field's actual name from the struct, when able to determine.
222222
func (fe *fieldError) StructField() string {
223223
// return fe.structField
224224
return fe.structNs[len(fe.structNs)-int(fe.structfieldLen):]
225225
}
226226

227-
// Value returns the actual fields value in case needed for creating the error
227+
// Value returns the actual field's value in case needed for creating the error
228228
// message
229229
func (fe *fieldError) Value() interface{} {
230230
return fe.value

regexes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
latitudeRegexString = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
3737
longitudeRegexString = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
3838
sSNRegexString = `^[0-9]{3}[ -]?(0[1-9]|[1-9][0-9])[ -]?([1-9][0-9]{3}|[0-9][1-9][0-9]{2}|[0-9]{2}[1-9][0-9]|[0-9]{3}[1-9])$`
39-
hostnameRegexStringRFC952 = `^[a-zA-Z][a-zA-Z0-9\-\.]+[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952
39+
hostnameRegexStringRFC952 = `^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$` // https://tools.ietf.org/html/rfc952
4040
hostnameRegexStringRFC1123 = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*?$` // accepts hostname starting with a digit https://tools.ietf.org/html/rfc1123
4141
btcAddressRegexString = `^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$` // bitcoin address
4242
btcAddressUpperRegexStringBech32 = `^BC1[02-9AC-HJ-NP-Z]{7,76}$` // bitcoin bech32 address https://en.bitcoin.it/wiki/Bech32

0 commit comments

Comments
 (0)