basic common app packages
This commit is contained in:
29
validate/validate.go
Normal file
29
validate/validate.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright 2024 Patial Tech (Ankit Patial).
|
||||
// All rights reserved.
|
||||
|
||||
package validate
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
var validate *validator.Validate
|
||||
|
||||
func init() {
|
||||
validate = validator.New()
|
||||
// register function to get tag name from json tags.
|
||||
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
|
||||
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
|
||||
if name == "-" {
|
||||
return ""
|
||||
}
|
||||
return name
|
||||
})
|
||||
}
|
||||
|
||||
func Struct(s any) error {
|
||||
return validate.Struct(s)
|
||||
}
|
Reference in New Issue
Block a user