Skip to content

Commit

Permalink
document thread safety and recommended singleton usage (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrascz committed Aug 8, 2021
1 parent a67baa7 commit 42525d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc.go
Expand Up @@ -7,6 +7,14 @@ and has the ability to dive into arrays and maps of any type.
see more examples https://github.com/go-playground/validator/tree/master/_examples
Singleton
Validator is designed to be thread-safe and used as a singleton instance.
It caches information about your struct and validations,
in essence only parsing your validation tags once per struct type.
Using multiple instances neglects the benefit of caching.
The not thread-safe functions are explicitly marked as such in the documentation.
Validation Functions Return Type error
Doing things this way is actually the way the standard library does, see the
Expand Down
4 changes: 4 additions & 0 deletions validator_instance.go
Expand Up @@ -89,6 +89,10 @@ type Validate struct {
}

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

tc := new(tagCache)
Expand Down

0 comments on commit 42525d8

Please sign in to comment.