Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document thread safety and recommended singleton usage #809

Merged
merged 2 commits into from Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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