diff --git a/go.mod b/go.mod index 15137a2..5b37719 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,10 @@ go 1.24.2 require ( github.com/json-iterator/go v1.1.12 - github.com/spf13/cast v1.7.1 + github.com/spf13/cast v1.8.0 github.com/spf13/viper v1.20.1 - golang.org/x/crypto v0.37.0 - golang.org/x/text v0.24.0 + golang.org/x/crypto v0.38.0 + golang.org/x/text v0.25.0 ) require ( @@ -22,6 +22,6 @@ require ( github.com/spf13/pflag v1.0.6 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/sys v0.32.0 // indirect + golang.org/x/sys v0.33.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index fbaa41c..33883c9 100644 --- a/go.sum +++ b/go.sum @@ -35,6 +35,8 @@ github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA= github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo= github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cast v1.8.0 h1:gEN9K4b8Xws4EX0+a0reLmhq8moKn7ntRlQYgjPeCDk= +github.com/spf13/cast v1.8.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4= @@ -49,10 +51,16 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE= golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc= +golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= +golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= +golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= +golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/utils/is.go b/utils/is.go index d941d93..102a005 100644 --- a/utils/is.go +++ b/utils/is.go @@ -13,25 +13,19 @@ type IsClass struct{} // Email - 是否为邮箱 func (this *IsClass) Email(email any) (ok bool) { - if email == nil { - return false - } + if email == nil { return false } return regexp.MustCompile(`\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}`).MatchString(cast.ToString(email)) } // Phone - 是否为手机号 func (this *IsClass) Phone(phone any) (ok bool) { - if phone == nil { - return false - } + if phone == nil { return false } return regexp.MustCompile(`^1[3456789]\d{9}$`).MatchString(cast.ToString(phone)) } // Mobile - 是否为手机号 func (this *IsClass) Mobile(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^1[3456789]\d{9}$`).MatchString(cast.ToString(value)) } @@ -39,19 +33,21 @@ func (this *IsClass) Mobile(value any) (ok bool) { func (this *IsClass) Empty(args ...any) (ok bool) { for _, value := range args { - if _, empty := typeof(value); !empty { - return false - } + if _, empty := typeof(value); !empty { return false } } return true } +// Null - 是否为 nil +func (this *IsClass) Null(value any) (ok bool) { + if value == nil { return true } + return false +} + // Domain - 是否为域名 func (this *IsClass) Domain(domain any) (ok bool) { - if domain == nil { - return false - } + if domain == nil { return false } return regexp.MustCompile(`^((https|http|ftp|rtsp|mms)?://)\S+`).MatchString(cast.ToString(domain)) } @@ -67,17 +63,13 @@ func (this *IsClass) False(value any) (ok bool) { // Number - 是否为数字 func (this *IsClass) Number(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[0-9]+$`).MatchString(cast.ToString(value)) } // Float - 是否为浮点数 func (this *IsClass) Float(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[0-9]+(.[0-9]+)?$`).MatchString(cast.ToString(value)) } @@ -88,224 +80,168 @@ func (this *IsClass) Bool(value any) (ok bool) { // Accepted - 验证某个字段是否为为 yes, on, 或是 1 func (this *IsClass) Accepted(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^(yes|on|1)$`).MatchString(cast.ToString(value)) } // Date - 是否为日期类型 func (this *IsClass) Date(date any) (ok bool) { - if date == nil { - return false - } + if date == nil { return false } return regexp.MustCompile(`^\d{4}-\d{1,2}-\d{1,2}$`).MatchString(cast.ToString(date)) } // Alpha - 只能包含字母 func (this *IsClass) Alpha(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[a-zA-Z]+$`).MatchString(cast.ToString(value)) } // AlphaNum - 只能包含字母和数字 func (this *IsClass) AlphaNum(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(cast.ToString(value)) } // AlphaDash - 只能包含字母、数字和下划线_及破折号- func (this *IsClass) AlphaDash(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(cast.ToString(value)) } // Chs - 是否为汉字 func (this *IsClass) Chs(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}]+$`).MatchString(cast.ToString(value)) } // ChsAlpha - 只能是汉字、字母 func (this *IsClass) ChsAlpha(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z]+$`).MatchString(cast.ToString(value)) } // ChsAlphaNum - 只能是汉字、字母和数字 func (this *IsClass) ChsAlphaNum(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$`).MatchString(cast.ToString(value)) } // ChsDash - 只能是汉字、字母、数字和下划线_及破折号- func (this *IsClass) ChsDash(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[\x{4e00}-\x{9fa5}a-zA-Z0-9_-]+$`).MatchString(cast.ToString(value)) } // Cntrl - 是否为控制字符 - (换行、缩进、空格) func (this *IsClass) Cntrl(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[\x00-\x1F\x7F]+$`).MatchString(cast.ToString(value)) } // Graph - 是否为可见字符 - (除空格外的所有可打印字符) func (this *IsClass) Graph(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[\x21-\x7E]+$`).MatchString(cast.ToString(value)) } // Lower - 是否为小写字母 func (this *IsClass) Lower(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[a-z]+$`).MatchString(cast.ToString(value)) } // Upper - 是否为大写字母 func (this *IsClass) Upper(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[A-Z]+$`).MatchString(cast.ToString(value)) } // Space - 是否为空白字符 - (空格、制表符、换页符等) func (this *IsClass) Space(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[\s]+$`).MatchString(cast.ToString(value)) } // Xdigit - 是否为十六进制字符 - (0-9、a-f、A-F) func (this *IsClass) Xdigit(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[\da-fA-F]+$`).MatchString(cast.ToString(value)) } // ActiveUrl - 是否为有效的域名或者IP func (this *IsClass) ActiveUrl(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^([a-z0-9-]+\.)+[a-z]{2,6}$`).MatchString(cast.ToString(value)) } // Ip - 是否为IP func (this *IsClass) Ip(ip any) (ok bool) { - if ip == nil { - return false - } + if ip == nil { return false } return regexp.MustCompile(`(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)`).MatchString(cast.ToString(ip)) } // Url - 是否为URL func (this *IsClass) Url(url any) (ok bool) { - if url == nil { - return false - } + if url == nil { return false } return regexp.MustCompile(`^((https|http|ftp|rtsp|mms)?://)\S+`).MatchString(cast.ToString(url)) } // IdCard - 是否为有效的身份证号码 func (this *IsClass) IdCard(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)`).MatchString(cast.ToString(value)) } // MacAddr - 是否为有效的MAC地址 func (this *IsClass) MacAddr(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2}$`).MatchString(cast.ToString(value)) } // Zip - 是否为有效的邮政编码 func (this *IsClass) Zip(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[1-9]\d{5}$`).MatchString(cast.ToString(value)) } // String - 是否为字符串 func (this *IsClass) String(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return reflect.TypeOf(value).Kind() == reflect.String } // Slice - 是否为切片 func (this *IsClass) Slice(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return reflect.TypeOf(value).Kind() == reflect.Slice } // Array - 是否为数组 func (this *IsClass) Array(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return reflect.TypeOf(value).Kind() == reflect.Array } // JsonString - 是否为json字符串 func (this *IsClass) JsonString(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return regexp.MustCompile(`^[{\[].*[}\]]$`).MatchString(cast.ToString(value)) } // Map - 是否为map func (this *IsClass) Map(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return reflect.TypeOf(value).Kind() == reflect.Map } // SliceSlice - 是否为二维切片 func (this *IsClass) SliceSlice(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return reflect.TypeOf(value).Kind() == reflect.Slice && reflect.TypeOf(value).Elem().Kind() == reflect.Slice } // MapAny - 是否为[]map[string]any func (this *IsClass) MapAny(value any) (ok bool) { - if value == nil { - return false - } + if value == nil { return false } return reflect.TypeOf(value).Kind() == reflect.Map && reflect.TypeOf(value).Elem().Kind() == reflect.Interface }