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

why always new Validator? #72

Open
54cheng opened this issue Feb 19, 2023 · 0 comments
Open

why always new Validator? #72

54cheng opened this issue Feb 19, 2023 · 0 comments

Comments

@54cheng
Copy link

54cheng commented Feb 19, 2023

github.com/marmotedu/api/apiserver/v1/validate.go

func (u *User) Validate() field.ErrorList {
val := validation.NewValidator(u)
......
}

func (u *User) ValidateUpdate() field.ErrorList {
val := validation.NewValidator(u)
.......
}

// Validate validates that a secret object is valid.
func (s *Secret) Validate() field.ErrorList {
val := validation.NewValidator(s)

return val.Validate()

}

// Validate validates that a policy object is valid.
func (p *Policy) Validate() field.ErrorList {
val := validation.NewValidator(p)

return val.Validate()

}

in the end
func NewValidator(data interface{}) *Validator {
result := validator.New()
......
}
//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.
validator.New()

l think result can be single instance,there is no need to create an validator instance everytime NewValidator function is called
Am l right?

@54cheng 54cheng changed the title why always new NewValidator? why always new Validator? Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant