Skip to content

Commit 71b5c77

Browse files
authored
Merge branch 'go-playground:master' into stricter-unicode-space-check-in-email-regex
2 parents 5cdcd60 + 0e3e2f9 commit 71b5c77

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+11323
-2831
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Bug Report
2+
description: File a bug report
3+
title: "[Bug]: "
4+
labels: ["bug"]
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: |
9+
Thanks for taking the time to fill out this bug report!
10+
- type: textarea
11+
id: what-happened
12+
attributes:
13+
label: What happened?
14+
description: Also tell us, what did you expect to happen?
15+
validations:
16+
required: true
17+
- type: textarea
18+
id: version
19+
attributes:
20+
label: Version
21+
description: What version of validator are you running?
22+
validations:
23+
required: true
24+
- type: textarea
25+
id: code
26+
attributes:
27+
label: Example Code
28+
description: Please provide a code example that demonstrates the issue
29+
render: go
30+
validations:
31+
required: true

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 2
2+
updates:
3+
# Maintain dependencies for Golang
4+
- package-ecosystem: gomod
5+
directory: "/"
6+
schedule:
7+
interval: weekly
8+
# Maintain dependencies for GitHub Actions
9+
- package-ecosystem: github-actions
10+
directory: "/"
11+
schedule:
12+
interval: weekly

.github/workflows/workflow.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
test:
99
strategy:
1010
matrix:
11-
go-version: [1.17.x,1.18.x,1.21.x,1.22.x]
11+
go-version: [1.22.x,1.23.x, 1.24.x]
1212
os: [ubuntu-latest, macos-latest, windows-latest]
1313
runs-on: ${{ matrix.os }}
1414
steps:
@@ -24,7 +24,7 @@ jobs:
2424
run: go test -race -covermode=atomic -coverprofile="profile.cov" ./...
2525

2626
- name: Send Coverage
27-
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.22.x'
27+
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.23.x'
2828
uses: shogo82148/actions-goveralls@v1
2929
with:
3030
path-to-profile: profile.cov
@@ -36,8 +36,8 @@ jobs:
3636
- uses: actions/checkout@v4
3737
- uses: actions/setup-go@v5
3838
with:
39-
go-version: 1.22.x
39+
go-version: 1.24.x
4040
- name: golangci-lint
41-
uses: golangci/golangci-lint-action@v4
41+
uses: golangci/golangci-lint-action@v8
4242
with:
4343
version: latest

.golangci.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
version: "2"
2+
linters:
3+
default: all
4+
disable:
5+
- copyloopvar
6+
- cyclop
7+
- depguard
8+
- dogsled
9+
- dupl
10+
- dupword
11+
- err113
12+
- errorlint
13+
- exhaustive
14+
- exhaustruct
15+
- forbidigo
16+
- forcetypeassert
17+
- funlen
18+
- gochecknoglobals
19+
- gocognit
20+
- goconst
21+
- gocritic
22+
- gocyclo
23+
- godot
24+
- gosec
25+
- gosmopolitan
26+
- interfacebloat
27+
- intrange
28+
- ireturn
29+
- lll
30+
- maintidx
31+
- misspell
32+
- mnd
33+
- nakedret
34+
- nestif
35+
- nilnil
36+
- nlreturn
37+
- nonamedreturns
38+
- paralleltest
39+
- perfsprint
40+
- prealloc
41+
- recvcheck
42+
- revive
43+
- staticcheck
44+
- tagalign
45+
- tagliatelle
46+
- testpackage
47+
- thelper
48+
- tparallel
49+
- unparam
50+
- varnamelen
51+
- wrapcheck
52+
- wsl

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ GOCMD=go
33
linters-install:
44
@golangci-lint --version >/dev/null 2>&1 || { \
55
echo "installing linting tools..."; \
6-
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.41.1; \
6+
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v2.0.2; \
77
}
88

99
lint: linters-install

README.md

Lines changed: 89 additions & 67 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/go-playground/locales/en"
10+
"github.com/go-playground/locales/zh"
11+
"github.com/go-playground/locales/zh_Hant_TW"
12+
ut "github.com/go-playground/universal-translator"
13+
"github.com/go-playground/validator/v10"
14+
en_translations "github.com/go-playground/validator/v10/translations/en"
15+
zh_translations "github.com/go-playground/validator/v10/translations/zh"
16+
zh_tw_translations "github.com/go-playground/validator/v10/translations/zh_tw"
17+
)
18+
19+
var uni *ut.UniversalTranslator
20+
21+
// This example showcases how to use the Validator and UniversalTranslator with both Simplified and Traditional Chinese languages.
22+
// To run the example:
23+
// Step 1: go run _examples/http-transalations/main.go
24+
// Step 2 - Simplified Chinese: curl -d '{"first_name":"foo"}' -H "Accept-Language: zh" -H "Content-Type: application/json" -X POST http://localhost:8081/users
25+
// Step 3 - Traditional Chinese: curl -d '{"first_name":"foo"}' -H "Accept-Language: zh-Hant-TW" -H "Content-Type: application/json" -X POST http://localhost:8081/users
26+
func main() {
27+
validate := validator.New()
28+
en := en.New()
29+
uni = ut.New(en, en, zh.New(), zh_Hant_TW.New())
30+
31+
validate = validator.New()
32+
enTrans, _ := uni.GetTranslator("en")
33+
en_translations.RegisterDefaultTranslations(validate, enTrans)
34+
zhTrans, _ := uni.GetTranslator("zh")
35+
zh_translations.RegisterDefaultTranslations(validate, zhTrans)
36+
zhHantTrans, _ := uni.GetTranslator("zh_Hant_TW")
37+
zh_tw_translations.RegisterDefaultTranslations(validate, zhHantTrans)
38+
39+
type User struct {
40+
FirstName string `json:"first_name" validate:"required"`
41+
LastName string `json:"last_name" validate:"required"`
42+
}
43+
44+
http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
45+
// ... fill user value
46+
var user User
47+
48+
// Header Accept-Language value is en or zh
49+
trans, _ := uni.GetTranslator(strings.Replace(r.Header.Get("Accept-Language"), "-", "_", -1))
50+
if err := validate.Struct(&user); err != nil {
51+
var errs validator.ValidationErrors
52+
var httpErrors []validator.ValidationErrorsTranslations
53+
if errors.As(err, &errs) {
54+
httpErrors = append(httpErrors, errs.Translate(trans))
55+
}
56+
r, _ := json.Marshal(httpErrors)
57+
w.Write(r)
58+
}
59+
})
60+
61+
http.ListenAndServe(":8081", nil)
62+
}

_examples/simple/main.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/go-playground/validator/v10"
@@ -61,24 +62,27 @@ func validateStruct() {
6162
// this check is only needed when your code could produce
6263
// an invalid value for validation such as interface with nil
6364
// value most including myself do not usually have code like this.
64-
if _, ok := err.(*validator.InvalidValidationError); ok {
65+
var invalidValidationError *validator.InvalidValidationError
66+
if errors.As(err, &invalidValidationError) {
6567
fmt.Println(err)
6668
return
6769
}
6870

69-
for _, err := range err.(validator.ValidationErrors) {
70-
71-
fmt.Println(err.Namespace())
72-
fmt.Println(err.Field())
73-
fmt.Println(err.StructNamespace())
74-
fmt.Println(err.StructField())
75-
fmt.Println(err.Tag())
76-
fmt.Println(err.ActualTag())
77-
fmt.Println(err.Kind())
78-
fmt.Println(err.Type())
79-
fmt.Println(err.Value())
80-
fmt.Println(err.Param())
81-
fmt.Println()
71+
var validateErrs validator.ValidationErrors
72+
if errors.As(err, &validateErrs) {
73+
for _, e := range validateErrs {
74+
fmt.Println(e.Namespace())
75+
fmt.Println(e.Field())
76+
fmt.Println(e.StructNamespace())
77+
fmt.Println(e.StructField())
78+
fmt.Println(e.Tag())
79+
fmt.Println(e.ActualTag())
80+
fmt.Println(e.Kind())
81+
fmt.Println(e.Type())
82+
fmt.Println(e.Value())
83+
fmt.Println(e.Param())
84+
fmt.Println()
85+
}
8286
}
8387

8488
// from here you can create your own error messages in whatever language you wish

_examples/struct-level/main.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"reflect"
78
"strings"
@@ -114,33 +115,37 @@ func main() {
114115
// this check is only needed when your code could produce
115116
// an invalid value for validation such as interface with nil
116117
// value most including myself do not usually have code like this.
117-
if _, ok := err.(*validator.InvalidValidationError); ok {
118+
var invalidValidationError *validator.InvalidValidationError
119+
if errors.As(err, &invalidValidationError) {
118120
fmt.Println(err)
119121
return
120122
}
121123

122-
for _, err := range err.(validator.ValidationErrors) {
123-
e := validationError{
124-
Namespace: err.Namespace(),
125-
Field: err.Field(),
126-
StructNamespace: err.StructNamespace(),
127-
StructField: err.StructField(),
128-
Tag: err.Tag(),
129-
ActualTag: err.ActualTag(),
130-
Kind: fmt.Sprintf("%v", err.Kind()),
131-
Type: fmt.Sprintf("%v", err.Type()),
132-
Value: fmt.Sprintf("%v", err.Value()),
133-
Param: err.Param(),
134-
Message: err.Error(),
124+
var validateErrs validator.ValidationErrors
125+
if errors.As(err, &validateErrs) {
126+
for _, err := range validateErrs {
127+
e := validationError{
128+
Namespace: err.Namespace(),
129+
Field: err.Field(),
130+
StructNamespace: err.StructNamespace(),
131+
StructField: err.StructField(),
132+
Tag: err.Tag(),
133+
ActualTag: err.ActualTag(),
134+
Kind: fmt.Sprintf("%v", err.Kind()),
135+
Type: fmt.Sprintf("%v", err.Type()),
136+
Value: fmt.Sprintf("%v", err.Value()),
137+
Param: err.Param(),
138+
Message: err.Error(),
139+
}
140+
141+
indent, err := json.MarshalIndent(e, "", " ")
142+
if err != nil {
143+
fmt.Println(err)
144+
panic(err)
145+
}
146+
147+
fmt.Println(string(indent))
135148
}
136-
137-
indent, err := json.MarshalIndent(e, "", " ")
138-
if err != nil {
139-
fmt.Println(err)
140-
panic(err)
141-
}
142-
143-
fmt.Println(string(indent))
144149
}
145150

146151
// from here you can create your own error messages in whatever language you wish

0 commit comments

Comments
 (0)