Skip to content

Commit 42525d8

Browse files
authored
document thread safety and recommended singleton usage (#809)
1 parent a67baa7 commit 42525d8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and has the ability to dive into arrays and maps of any type.
77
88
see more examples https://github.com/go-playground/validator/tree/master/_examples
99
10+
Singleton
11+
12+
Validator is designed to be thread-safe and used as a singleton instance.
13+
It caches information about your struct and validations,
14+
in essence only parsing your validation tags once per struct type.
15+
Using multiple instances neglects the benefit of caching.
16+
The not thread-safe functions are explicitly marked as such in the documentation.
17+
1018
Validation Functions Return Type error
1119
1220
Doing things this way is actually the way the standard library does, see the

validator_instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ type Validate struct {
8989
}
9090

9191
// New returns a new instance of 'validate' with sane defaults.
92+
// Validate is designed to be thread-safe and used as a singleton instance.
93+
// It caches information about your struct and validations,
94+
// in essence only parsing your validation tags once per struct type.
95+
// Using multiple instances neglects the benefit of caching.
9296
func New() *Validate {
9397

9498
tc := new(tagCache)

0 commit comments

Comments
 (0)