51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package cms
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"gopkg.in/go-playground/validator.v9"
|
||
|
||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||
"git.rosy.net.cn/jx-callback/business/model"
|
||
)
|
||
|
||
var (
|
||
unitNamesMap map[string]int
|
||
specUnitNamesMap map[string]int
|
||
validate = validator.New()
|
||
)
|
||
|
||
func init() {
|
||
unitNamesMap = jxutils.MakeValidationMapFromSlice(model.UnitNames, 1)
|
||
specUnitNamesMap = jxutils.MakeValidationMapFromSlice(model.SpecUnitNames, 1)
|
||
}
|
||
|
||
func validateStringInMap(name string, value interface{}, flagMap map[string]int) (err error) {
|
||
if strValue, ok := value.(string); ok {
|
||
if flagMap[strValue] == 1 {
|
||
return nil
|
||
}
|
||
}
|
||
return fmt.Errorf("属性%s类型或取值不合法,要求string", name)
|
||
}
|
||
|
||
func ValidateUnit(value interface{}) (err error) {
|
||
return validateStringInMap("Unit", value, unitNamesMap)
|
||
}
|
||
|
||
func ValidateSpecUnit(value interface{}) (err error) {
|
||
return validateStringInMap("SpecUnit", value, specUnitNamesMap)
|
||
}
|
||
|
||
func ValidateStruct(value interface{}) (err error) {
|
||
return validate.Struct(value)
|
||
}
|
||
|
||
func ValidateVar(value interface{}, tag string) (err error) {
|
||
return validate.Var(value, tag)
|
||
}
|
||
|
||
func ValidateStructPartial(value interface{}, fields ...string) (err error) {
|
||
return validate.StructPartial(value, fields...)
|
||
}
|