diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 4ea884e..2ad9e1e 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -36,25 +36,20 @@ jobs: # a pull request then we can checkout the head. fetch-depth: 2 - # If this run was triggered by a pull request event, then checkout - # the head of the pull request instead of the merge commit. - - run: git checkout HEAD^2 - if: ${{ github.event_name == 'pull_request' }} - # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. + # By default, queries listed here will override any specified in a config file. # Prefix the list here with "+" to use these queries and those in the config file. # queries: ./path/to/local/query, your-org/your-repo/queries@main # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v2 + uses: github/codeql-action/autobuild@v3 # ℹ️ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -68,4 +63,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 diff --git a/validator/cmp.go b/validator/cmp.go index ed29bd6..f80afa5 100644 --- a/validator/cmp.go +++ b/validator/cmp.go @@ -11,7 +11,6 @@ func Between[T cmp.Ordered](min, max T) func(T) bool { if max < min { panic("max 必须大于等于 min") } - return func(val T) bool { return val > min && val < max } } @@ -20,25 +19,16 @@ func BetweenEqual[T cmp.Ordered](min, max T) func(T) bool { if max < min { panic("max 必须大于等于 min") } - return func(val T) bool { return val >= min && val <= max } } -func Less[T cmp.Ordered](num T) func(T) bool { - return func(t T) bool { return t < num } -} +func Less[T cmp.Ordered](num T) func(T) bool { return func(t T) bool { return t < num } } -func LessEqual[T cmp.Ordered](num T) func(T) bool { - return func(t T) bool { return t <= num } -} +func LessEqual[T cmp.Ordered](num T) func(T) bool { return func(t T) bool { return t <= num } } -func Great[T cmp.Ordered](num T) func(T) bool { - return func(t T) bool { return t > num } -} +func Great[T cmp.Ordered](num T) func(T) bool { return func(t T) bool { return t > num } } -func GreatEqual[T cmp.Ordered](num T) func(T) bool { - return func(t T) bool { return t >= num } -} +func GreatEqual[T cmp.Ordered](num T) func(T) bool { return func(t T) bool { return t >= num } } // HTTPStatus 是否为有效的 HTTP 状态码 func HTTPStatus(s int) bool { return BetweenEqual(100, 599)(s) } diff --git a/validator/string.go b/validator/string.go index 59dbf7a..1c99205 100644 --- a/validator/string.go +++ b/validator/string.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/google/uuid" + "github.com/issue9/webfilter/gb11643" "github.com/issue9/webfilter/gb32100" "github.com/issue9/webfilter/internal/isbn" @@ -48,6 +49,9 @@ func IP6(val string) bool { return err == nil && ip.Is6() } +// IP 判断是否 IP 地址 +// +// 同时支持 [IP4] 和 [IP6] func IP(val string) bool { _, err := netip.ParseAddr(val) return err == nil @@ -55,7 +59,7 @@ func IP(val string) bool { // ISBN 判断是否为合法的 [ISBN] 串号 // -// 可以同时判断 ISBN10 和 ISBN13 +// 可以同时判断 [ISBN10] 和 [ISBN13] // // [ISBN]: https://zh.wikipedia.org/wiki/%E5%9B%BD%E9%99%85%E6%A0%87%E5%87%86%E4%B9%A6%E5%8F%B7 func ISBN(val string) bool { return isbn.IsValid([]byte(val)) } @@ -132,11 +136,6 @@ func Alpha(s string) bool { return true } -// EmptyOr 空字符串或是非空的情况下满足 v 的条件 -func EmptyOr(v func(string) bool) func(string) bool { - return Or(func(s string) bool { return s == "" }, v) -} - // CNMobile 验证中国大陆的手机号码 func CNMobile(val string) bool { // 可选的 0,86,086,+86 @@ -175,3 +174,6 @@ func Digit(val string) bool { // UUID 验证 UUID 格式是否正确 func UUID(val string) bool { return uuid.Validate(val) == nil } + +// Empty 字符串是否为空 +func Empty(val string) bool { return val == "" } diff --git a/validator/validator.go b/validator/validator.go index d64b02a..e5fc93e 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -53,19 +53,18 @@ func Or[T any](v ...func(T) bool) func(T) bool { } // Not 验证器的取反 -func Not[T any](v func(T) bool) func(T) bool { - return func(val T) bool { return !v(val) } -} +func Not[T any](v func(T) bool) func(T) bool { return func(val T) bool { return !v(val) } } +// Zero 是否为零值 +// +// 采用 [reflect.Value.IsZero] 判断。 func Zero[T any](v T) bool { return reflect.ValueOf(v).IsZero() } // Equal 生成判断值是否等于 v 的验证器 -func Equal[T comparable](v T) func(T) bool { - return func(t T) bool { return t == v } -} +func Equal[T comparable](v T) func(T) bool { return func(t T) bool { return t == v } } -func NilOr[T any](v func(T) bool) func(T) bool { - return Or(func(v T) bool { return reflect.ValueOf(v).IsNil() }, v) -} +// Nil 是否为 nil +func Nil[T any](v T) bool { return reflect.ValueOf(v).IsNil() } +// JSON 验证是否为正确的 JSON 内容 func JSON(val []byte) bool { return json.Valid(val) } diff --git a/webfilter.go b/webfilter.go index 2c08632..065f74d 100644 --- a/webfilter.go +++ b/webfilter.go @@ -4,5 +4,5 @@ // Package webfilter 适合 [web.filter] 的过滤器 // -// [web/filter]: https://github.com/issue9/web +// [web/filter]: https://github.com/issue9/web/filter package webfilter