-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/issue9/assert/v4 v4.1.1 h1:OhPE8SB8n/qZCNGLQa+6MQtr/B3oON0JAVj68k8jJlc= | ||
github.com/issue9/assert/v4 v4.1.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4= | ||
github.com/issue9/cache v0.9.2 h1:tMvXPhGKBXbSIMHi5qzsdwmQjf7qGYBo1W/LLTDHw9s= | ||
github.com/issue9/cache v0.9.2/go.mod h1:gFTzhzGGAc6Dpon9fbWtJNCjeG3QiLs+bFc8j5z98Qg= | ||
github.com/issue9/cache v0.10.0 h1:pgw4kbAFUeUu9pOKcNSdc+BhhXwdRFzjwAvWAX2BWHc= | ||
github.com/issue9/cache v0.10.0/go.mod h1:0s9j7qiKv4uWYqz0D2N2H7bIBvmtD+903h5GqnxW6i4= | ||
github.com/issue9/config v0.6.2 h1:znXvsk6gh0wm+fTEn0zUjjramKuOLY8Jt0ZTxp4GIkc= | ||
github.com/issue9/config v0.6.2/go.mod h1:S97FVtZim4rPB+M49EoOuBajaqY4ULQbqltZmZ99+Dc= | ||
github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw= | ||
github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A= | ||
github.com/issue9/localeutil v0.26.5 h1:e78b6cOOtgzfb4g4U9uPLC8QyK6Lux+s7ZiQe+6iM1A= | ||
github.com/issue9/localeutil v0.26.5/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8= | ||
github.com/issue9/scheduled v0.19.3 h1:hI/8gh1SMH62inPVsrZazRBxc91RQT/YbnWr1S9se08= | ||
github.com/issue9/scheduled v0.19.3/go.mod h1:4OTuwC3QMRJLGRV8ImmRS2bqX58bDjKi2OAuzHcgJuQ= | ||
github.com/issue9/web v0.87.4 h1:BD6+ZjSCnlYS6G8j0xOJxmjsHkhaOAPP0Oe5OW3z+3M= | ||
github.com/issue9/web v0.87.4/go.mod h1:5nkr+8J/LD1WAyjA/4gmWgi3IOxRV37cR6c3EJ062b8= | ||
github.com/issue9/version v1.0.8 h1:IsNdDYdV8UGDGwwgp8H4RszJE0Ko26HjWg9pZzyOivs= | ||
github.com/issue9/version v1.0.8/go.mod h1:w8bQwODBOG5+iaS3qIJbElxxpp3Uo4x5F39qKBqwpdc= | ||
github.com/issue9/web v0.87.7 h1:Z2EUzAs/84x3EFZr7EuTxnlXcHE1X2of67eIq+7vfaE= | ||
github.com/issue9/web v0.87.7/go.mod h1:6RK59GaungHYQgTx4WDSSE0O9GJTT4RynhXgiQnPzJo= | ||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// SPDX-FileCopyrightText: 2022-2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package validator | ||
|
||
import "github.com/issue9/version" | ||
|
||
// Semver [semver] 版本号验证 | ||
// | ||
// [semver]: https://semver.org/lang/zh-CN/ | ||
func Semver(ver string) bool { return version.SemVerValid(ver) } | ||
|
||
// SemverCompatible 创建一个验证 [semver] 是否兼容 val 的验证器 | ||
func SemverCompatible(ver string) func(string) bool { | ||
semver, err := version.SemVer(ver) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return func(v string) bool { | ||
ok, err := semver.CompatibleString(v) | ||
return err == nil && ok | ||
} | ||
} | ||
|
||
// SemverGreat 判断版本号是否大于 ver | ||
func SemverGreat(ver string) func(string) bool { | ||
semver, err := version.SemVer(ver) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return func(val string) bool { | ||
num, err := semver.CompareString(val) | ||
return err == nil && num < 0 | ||
} | ||
} | ||
|
||
func SemverGreatEqual(ver string) func(string) bool { | ||
semver, err := version.SemVer(ver) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return func(val string) bool { | ||
num, err := semver.CompareString(val) | ||
return err == nil && num <= 0 | ||
} | ||
} | ||
|
||
func SemverLess(ver string) func(string) bool { | ||
semver, err := version.SemVer(ver) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return func(val string) bool { | ||
num, err := semver.CompareString(val) | ||
return err == nil && num > 0 | ||
} | ||
} | ||
|
||
func SemverLessEqual(ver string) func(string) bool { | ||
semver, err := version.SemVer(ver) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return func(val string) bool { | ||
num, err := semver.CompareString(val) | ||
return err == nil && num >= 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-FileCopyrightText: 2022-2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package validator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/issue9/assert/v4" | ||
) | ||
|
||
func TestSemver(t *testing.T) { | ||
a := assert.New(t, false) | ||
a.True(Semver("1.0.0")). | ||
False(Not(Semver)("1.0.0")) | ||
} | ||
|
||
func TestSemverCompatible(t *testing.T) { | ||
a := assert.New(t, false) | ||
|
||
v := SemverCompatible("1.0.0") | ||
a.True(v("1.1.1")).False(v("2.0.1")) | ||
} | ||
|
||
func TestSemverGreat(t *testing.T) { | ||
a := assert.New(t, false) | ||
|
||
v := SemverGreat("1.0.0") | ||
a.True(v("1.1.1")).False(v("1.0.0")) | ||
|
||
v = SemverGreatEqual("1.0.0") | ||
a.True(v("1.1.1")).True(v("1.0.0")) | ||
} | ||
|
||
func TestSemverLess(t *testing.T) { | ||
a := assert.New(t, false) | ||
|
||
v := SemverLess("2.0.0") | ||
a.True(v("1.1.1")).False(v("2.0.0")) | ||
|
||
v = SemverLessEqual("2.0.0") | ||
a.True(v("1.1.1")).True(v("2.0.0")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters