2024-11-18 15:23:13 +00:00
|
|
|
// Copyright 2024 Patial Tech. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2024-11-15 16:12:15 +00:00
|
|
|
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)
|
|
|
|
}
|