245 lines
5.8 KiB
Go
245 lines
5.8 KiB
Go
package validator
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
var defaultNotNullValidator *NotNullValidator
|
|
|
|
type ValidateFunc func(param *JsonParam) (bool, error)
|
|
|
|
type NotNullValidator struct {
|
|
validators map[string]ValidateFunc
|
|
}
|
|
|
|
func InitDefaultNotNullValidator() {
|
|
defaultNotNullValidator = new(NotNullValidator)
|
|
defaultNotNullValidator.init()
|
|
}
|
|
|
|
func GetDefaultNotNullValidator() *NotNullValidator {
|
|
return defaultNotNullValidator
|
|
}
|
|
|
|
func (p *NotNullValidator) init() {
|
|
p.validators = make(map[string]ValidateFunc)
|
|
p.validators[StringStr] = p.validateString
|
|
p.validators[StructStr] = p.validateStruct
|
|
|
|
p.validators[BoolPtrStr] = p.validateBoolPtr
|
|
p.validators[IntPtrStr] = p.validateIntPtr
|
|
p.validators[Int64PtrStr] = p.validateInt64Ptr
|
|
p.validators[Float64PtrStr] = p.validateFloa64Ptr
|
|
p.validators[StringPtrStr] = p.validateStringPtr
|
|
p.validators[StructPtrStr] = p.validateStructPtr
|
|
|
|
p.validators[BoolSliceStr] = p.validateBoolSlice
|
|
p.validators[IntSliceStr] = p.validateIntSlice
|
|
p.validators[Int64SliceStr] = p.validateInt64Slice
|
|
p.validators[Float64SliceStr] = p.validateFloa64Slice
|
|
p.validators[StringSliceStr] = p.validateStringSlice
|
|
p.validators[StructSliceStr] = p.validateStructSlice
|
|
|
|
p.validators[BoolSlicePtrStr] = p.validateBoolSlicePtr
|
|
p.validators[IntSlicePtrStr] = p.validateIntSlicePtr
|
|
p.validators[Int64SlicePtrStr] = p.validateInt64SlicePtr
|
|
p.validators[Float64SlicePtrStr] = p.validateFloa64SlicePtr
|
|
p.validators[StringSlicePtrStr] = p.validateStringSlicePtr
|
|
}
|
|
|
|
// validate
|
|
func (p *NotNullValidator) Validate(notNullItem NotNullItem) error {
|
|
for _, param := range notNullItem.ProvideNotNullValue() {
|
|
|
|
var ok bool
|
|
var err error
|
|
|
|
// 参数类型是否静态确定,未静态确定的执行通用反射方法
|
|
if !param.IsTypeStaticallyDefinable {
|
|
ok, err = p.validateTypeUnknown(param)
|
|
} else {
|
|
// 用户是否自定义了鉴定方法,是则执行自定义方法,否则执行默认方法
|
|
if param.IsValidateFuncDefined {
|
|
ok, err = param.CustomizedValidateFunc(param)
|
|
} else {
|
|
ok, err = p.validators[param.TypeDesc](param)
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
//如果非空校验失败,则返回错误信息
|
|
if !ok {
|
|
return fmt.Errorf(param.ErrMsg)
|
|
}
|
|
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// string
|
|
func (p *NotNullValidator) validateString(param *JsonParam) (bool, error) {
|
|
if val, ok := param.Value.(string); ok {
|
|
if val == "" {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
return false, ErrInvalidAssert
|
|
}
|
|
|
|
// 指针
|
|
func (p *NotNullValidator) validateBoolPtr(param *JsonParam) (bool, error) {
|
|
if param.BoolPtrVal == nil {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateIntPtr(param *JsonParam) (bool, error) {
|
|
if param.IntPtrVal == nil {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateInt64Ptr(param *JsonParam) (bool, error) {
|
|
if param.Int64PtrVal == nil {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateFloa64Ptr(param *JsonParam) (bool, error) {
|
|
if param.Float64PtrVal == nil {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateStringPtr(param *JsonParam) (bool, error) {
|
|
if param.StringPtrVal == nil {
|
|
return false, nil
|
|
}
|
|
if *(param.StringPtrVal) == "" {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// 切片
|
|
func (p *NotNullValidator) validateBoolSlice(param *JsonParam) (bool, error) {
|
|
if len(param.BoolSliceVal) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateIntSlice(param *JsonParam) (bool, error) {
|
|
if len(param.IntSliceVal) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateInt64Slice(param *JsonParam) (bool, error) {
|
|
if len(param.Int64SliceVal) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateFloa64Slice(param *JsonParam) (bool, error) {
|
|
if len(param.Float64SliceVal) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateStringSlice(param *JsonParam) (bool, error) {
|
|
if len(param.StringSliceVal) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// 切片的指针
|
|
func (p *NotNullValidator) validateBoolSlicePtr(param *JsonParam) (bool, error) {
|
|
if param.BoolSlicePtrVal == nil {
|
|
return false, nil
|
|
}
|
|
if len(*(param.BoolSlicePtrVal)) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateIntSlicePtr(param *JsonParam) (bool, error) {
|
|
if param.IntSlicePtrVal == nil {
|
|
return false, nil
|
|
}
|
|
if len(*(param.IntSlicePtrVal)) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateInt64SlicePtr(param *JsonParam) (bool, error) {
|
|
if param.Int64SlicePtrVal == nil {
|
|
return false, nil
|
|
}
|
|
if len(*(param.Int64SlicePtrVal)) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateFloa64SlicePtr(param *JsonParam) (bool, error) {
|
|
if param.Float64SlicePtrVal == nil {
|
|
return false, nil
|
|
}
|
|
if len(*(param.Float64SlicePtrVal)) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateStringSlicePtr(param *JsonParam) (bool, error) {
|
|
if param.StringSlicePtrVal == nil {
|
|
return false, nil
|
|
}
|
|
if len(*(param.StringSlicePtrVal)) == 0 {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateTypeUnknown(param *JsonParam) (bool, error) {
|
|
if param.Value == nil {
|
|
return false, nil
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
func (p *NotNullValidator) validateStruct(param *JsonParam) (bool, error) {
|
|
if param.NullableValue == nil {
|
|
return false, nil
|
|
}
|
|
return !param.NullableValue.IsEmpty(), nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateStructPtr(param *JsonParam) (bool, error) {
|
|
if param.NullableValue == nil {
|
|
return false, nil
|
|
}
|
|
return !param.NullableValue.IsEmpty(), nil
|
|
}
|
|
|
|
func (p *NotNullValidator) validateStructSlice(param *JsonParam) (bool, error) {
|
|
return param.SliceLen > 0, nil
|
|
}
|