validate

package
v0.0.0-...-d8d3109 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Alpha = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaDash = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaDashSpace = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z_\- ]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNum = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNumeric = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNumericComma = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9,]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNumericDash = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNumericDashDot = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNumericDashSpace = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9_\- ]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNumericDashSpaceDot = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9_.\- ]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNumericDot = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9.]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaNumericSpace = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9 ]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var AlphaSpace = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z ]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var Base64 = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9+/]+={0,2}$`)
	return regex.MatchString(memberVal.String())
}
View Source
var Base64RawURL = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9._-]+={0,2}$`)
	return regex.MatchString(memberVal.String())
}
View Source
var Base64URL = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-zA-Z0-9-_]+={0,2}$`)
	return regex.MatchString(memberVal.String())
}
View Source
var CnPhone = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^1[3456789]\d{9}$`)
	return regex.MatchString(memberVal.String())
}
View Source
var Email = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	email := memberVal.String()
	if email == "" {
		return false
	}
	emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
	return emailRegex.MatchString(email)
}
View Source
var HSLAColor = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^hsla\((\d{1,3}),(\d{1,3})%,(\d{1,3})%,([01]|0\.\d+)\)$`)
	return regex.MatchString(memberVal.String())
}
View Source
var HSLColor = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^hsl\((\d{1,3}),(\d{1,3})%,(\d{1,3})%\)$`)
	return regex.MatchString(memberVal.String())
}
View Source
var HexColor = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$`)
	return regex.MatchString(memberVal.String())
}
View Source
var Hexadecimal = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[a-fA-F0-9]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var IPV4 = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^([0-9]{1,3}\.){3}[0-9]{1,3}$`)
	return regex.MatchString(memberVal.String())
}
View Source
var IPV6 = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$`)
	return regex.MatchString(memberVal.String())
}
View Source
var Len = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	i, err := strconv.Atoi(condition)
	if err != nil {
		panic(err)
	}
	switch memberVal.Kind() {
	case reflect.String:
		return len(memberVal.String()) == i
	case reflect.Slice, reflect.Array, reflect.Map:
		return memberVal.Len() == i
	default:
		return false
	}
}
View Source
var Max = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	i, err := strconv.Atoi(condition)
	if err != nil {
		panic(err)
	}
	switch memberVal.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return memberVal.Int() <= int64(i)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return memberVal.Uint() <= uint64(i)
	case reflect.Float32, reflect.Float64:
		return memberVal.Float() <= float64(i)
	default:
		return false
	}
}
View Source
var MaxLen = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	i, err := strconv.Atoi(condition)
	if err != nil {
		panic(err)
	}
	switch memberVal.Kind() {
	case reflect.String:
		return len(memberVal.String()) <= i
	case reflect.Slice, reflect.Array, reflect.Map:
		return memberVal.Len() <= i
	default:
		return false
	}
}
View Source
var Min = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	i, err := strconv.Atoi(condition)
	if err != nil {
		panic(err)
	}
	switch memberVal.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return memberVal.Int() >= int64(i)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return memberVal.Uint() >= uint64(i)
	case reflect.Float32, reflect.Float64:
		return memberVal.Float() >= float64(i)
	default:
		return false
	}
}
View Source
var MinLen = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	i, err := strconv.Atoi(condition)
	if err != nil {
		panic(err)
	}
	switch memberVal.Kind() {
	case reflect.String:
		return len(memberVal.String()) >= i
	case reflect.Slice, reflect.Array, reflect.Map:
		return memberVal.Len() >= i
	default:
		return false
	}
}
View Source
var Numeric = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[0-9]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var NumericComma = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[0-9,]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var NumericDash = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[0-9_-]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var NumericDashDot = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[0-9_.-]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var NumericDot = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^[0-9.]+$`)
	return regex.MatchString(memberVal.String())
}
View Source
var OneOf = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	splits := strings.Split(condition, "|")
	t := memberVal.Type().Kind()
	v := ""
	switch t {
	case reflect.Int:
		v = strconv.Itoa(int(memberVal.Int()))
	case reflect.String:
		v = memberVal.String()
	case reflect.Float32, reflect.Float64:
		v = strconv.FormatFloat(memberVal.Float(), 'f', -1, 64)
	default:
		return false
	}

	for _, s := range splits {
		if s == v {
			return true
		}
	}
	return false
}
View Source
var RGBAColor = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),([01]|0\.\d+)\)$`)
	return regex.MatchString(memberVal.String())
}
View Source
var RGBColor = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(`^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$`)
	return regex.MatchString(memberVal.String())
}
View Source
var Regex = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	regex := regexp.MustCompile(condition)
	return regex.MatchString(memberVal.String())
}
View Source
var Required = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	if memberVal.IsValid() && !memberVal.IsZero() {
		return true
	}
	return false
}
View Source
var SameAs = func(memberVal reflect.Value, condition string, structVal reflect.Value) bool {
	compareVal := structVal.FieldByName(condition)
	if !compareVal.IsValid() {
		return false
	}
	return memberVal.String() == compareVal.String()
}

Functions

This section is empty.

Types

type Checker

type Checker func(memberVal reflect.Value, condition string, structVal reflect.Value) bool

type IValidator

type IValidator interface {
	CheckStruct(structVar any) error
	CheckVar(param any, ValidStr string) bool
}

type Validator

type Validator struct {
	Checkers            map[string]Checker
	StructErrDefaultMsg string
	StructValidTag      string
	StructErrMsgTag     string
	DefaultErrorMsg     string
}

func New

func New() *Validator

func (*Validator) AddCheckers

func (v *Validator) AddCheckers(cs map[string]Checker)

func (*Validator) CheckStruct

func (v *Validator) CheckStruct(s any) error

func (*Validator) CheckVar

func (v *Validator) CheckVar(s any, validates string) bool

type VarChecker

type VarChecker func(memberVal reflect.Value, condition string) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL