From fa3f58c42f3afd755cd3d873f0ac475d303c0c3c Mon Sep 17 00:00:00 2001 From: caixw Date: Mon, 15 Jan 2024 09:41:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4=20issue9/validat?= =?UTF-8?q?ion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/test.yml | 2 +- go.mod | 11 +++++------ go.sum | 23 ++++++++++------------- internal/loader/config.go | 3 +-- internal/loader/is.go | 18 ++++++++++++++++++ internal/loader/loader.go | 7 +++---- 6 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 internal/loader/is.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2254caf..4b8f12e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,7 @@ jobs: git config --global core.eol lf - name: Check out code into the Go module directory - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Go ${{ matrix.go }} uses: actions/setup-go@v4 diff --git a/go.mod b/go.mod index b1e951b..e9fd95b 100644 --- a/go.mod +++ b/go.mod @@ -3,16 +3,15 @@ module github.com/caixw/blogit/v2 go 1.18 require ( - github.com/alecthomas/chroma/v2 v2.11.1 + github.com/alecthomas/chroma/v2 v2.12.0 github.com/fsnotify/fsnotify v1.7.0 github.com/issue9/assert/v3 v3.1.0 github.com/issue9/cmdopt v0.13.0 github.com/issue9/errwrap v0.3.1 - github.com/issue9/localeutil v0.24.0 + github.com/issue9/localeutil v0.26.1 github.com/issue9/sliceutil v0.15.0 - github.com/issue9/term/v3 v3.2.4 - github.com/issue9/validation v0.8.0 - github.com/issue9/version v1.0.6 + github.com/issue9/term/v3 v3.2.5 + github.com/issue9/version v1.0.7 github.com/mdigger/goldmark-toc v0.0.0-20191225162753-7bc0e0d778c3 github.com/psanford/memfs v0.0.0-20230130182539-4dbf7e3e865e github.com/yuin/goldmark v1.6.0 @@ -26,6 +25,6 @@ require ( github.com/dlclark/regexp2 v1.10.0 // indirect github.com/gosimple/slug v1.9.0 // indirect github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect - golang.org/x/sys v0.14.0 // indirect + golang.org/x/sys v0.15.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index f192be0..9ae57dd 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink= github.com/alecthomas/chroma/v2 v2.2.0/go.mod h1:vf4zrexSH54oEjJ7EdB65tGNHmH3pGZmVkgTP5RHvAs= -github.com/alecthomas/chroma/v2 v2.11.1 h1:m9uUtgcdAwgfFNxuqj7AIG75jD2YmL61BBIJWtdzJPs= -github.com/alecthomas/chroma/v2 v2.11.1/go.mod h1:4TQu7gdfuPjSh76j78ietmqh9LiurGF0EpseFXdKMBw= +github.com/alecthomas/chroma/v2 v2.12.0 h1:Wh8qLEgMMsN7mgyG8/qIpegky2Hvzr4By6gEF7cmWgw= +github.com/alecthomas/chroma/v2 v2.12.0/go.mod h1:4TQu7gdfuPjSh76j78ietmqh9LiurGF0EpseFXdKMBw= github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -17,23 +17,20 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/gosimple/slug v1.9.0 h1:r5vDcYrFz9BmfIAMC829un9hq7hKM4cHUrsv36LbEqs= github.com/gosimple/slug v1.9.0/go.mod h1:AMZ+sOVe65uByN3kgEyf9WEBKBCSS+dJjMX9x4vDJbg= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= -github.com/issue9/assert/v2 v2.3.2 h1:J8TxGeak/CvrMchXHjs9CFChsZvpX0hGE/HTnwT6L+8= github.com/issue9/assert/v3 v3.1.0 h1:oxLFXS7QnBKI4lB31pRoYO96yErkWAJtR7iv+LNjAPg= github.com/issue9/assert/v3 v3.1.0/go.mod h1:yft/uaskRpwQTyBT3n1zRl91SR1wNlO4fLZHzOa4bdM= github.com/issue9/cmdopt v0.13.0 h1:mLaa7R94a6MbCf+1GIt4YgFDW6fdt/nnL0AjgKgf8jQ= github.com/issue9/cmdopt v0.13.0/go.mod h1:l//IcugcBwX+vCc2KrgC4ylU6rEzhjQyxno7hWoLCDE= github.com/issue9/errwrap v0.3.1 h1:8g4lYJaGnoiXyZ1oZyH/7zPDGgw5RNiE9Q6ri9kE6Z8= github.com/issue9/errwrap v0.3.1/go.mod h1:HLR0e5iimd2aJXM9YrThOsRj3/6lMtk77lVp7zyvJ4E= -github.com/issue9/localeutil v0.24.0 h1:EnqsgkhWXqQXU+RM9ISAYnBaxxZYi3ec+6Z+AlxGga4= -github.com/issue9/localeutil v0.24.0/go.mod h1:JvTb8B/2oVEZU1VHrBJXPHlE/1gZJFRMcF/ziKD8JJY= +github.com/issue9/localeutil v0.26.1 h1:4T4508SlTKcKkNjaj6Fer+ZyMVOU+UBUu4DWk/Zq8+I= +github.com/issue9/localeutil v0.26.1/go.mod h1:JU83qFEhBswkZdLc3bWS719ku5QY1/mqU8vhsP9Znsw= github.com/issue9/sliceutil v0.15.0 h1:E6Xnl3FY5h0ZGNzyx1VEFAfGdParaq/BkX1QQR0uFwI= github.com/issue9/sliceutil v0.15.0/go.mod h1:n9meV7AamDhmehOBuV4GrxW3yw7O1cZmLx3Xizg1bps= -github.com/issue9/term/v3 v3.2.4 h1:bCY0uk8lLaY6XLbZdYQVl5e0CqJkGxV8EYJxJGFv8xU= -github.com/issue9/term/v3 v3.2.4/go.mod h1:f8L2Pdh6t/i1hfWFxrN9r2W1n4bNMEq/sKmS9RQSaAg= -github.com/issue9/validation v0.8.0 h1:kH2h70XK4ujijyPzfYw5ttAoefNhpnSK5AX8PNVSx+w= -github.com/issue9/validation v0.8.0/go.mod h1:6nBzRwLfihVfSfKbmno6P0UfoOnyQZc6Qw7CApwHRxE= -github.com/issue9/version v1.0.6 h1:9rzIpHEbmMjFmQkxDC9ciew9nfdTh7+VpO8ZyAbIhdk= -github.com/issue9/version v1.0.6/go.mod h1:gVP3/z/bPHoZuWJ+5021x+QkkmhHfFcRehn/I0RCiAo= +github.com/issue9/term/v3 v3.2.5 h1:kAC5ynwTiqe/63E3JAV2Cb7eVxVno+L7VsmYgJyyLw0= +github.com/issue9/term/v3 v3.2.5/go.mod h1:Qm0PvOTggMvZUEsAmpiBr/uBaq5Qh/w4LQJcvznfAjc= +github.com/issue9/version v1.0.7 h1:uKcWyuEWbcFdPeqmR2ivuy4/CsB0la0xkk3gxlMjxZs= +github.com/issue9/version v1.0.7/go.mod h1:YTMKrK5SRLf0xgXfCPy0T/vpjTP52gjor67fsEVcnMU= github.com/mdigger/goldmark-toc v0.0.0-20191225162753-7bc0e0d778c3 h1:V8Lm1RZWBBCemfs8u4aCkfrovo6TQWxCdIPbNFzZmZs= github.com/mdigger/goldmark-toc v0.0.0-20191225162753-7bc0e0d778c3/go.mod h1:8N/DY/esIgHhhFgb7Hqu6IImzuGUmc3rc2vJlkg7MVw= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -51,8 +48,8 @@ github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc h1:+ github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc/go.mod h1:ovIvrum6DQJA4QsJSovrkC4saKHQVs7TvcaeO8AIl5I= github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc= github.com/yuin/goldmark-meta v1.1.0/go.mod h1:U4spWENafuA7Zyg+Lj5RqK/MF+ovMYtBvXi1lBb2VP0= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/loader/config.go b/internal/loader/config.go index 5daf8dd..52d01f5 100644 --- a/internal/loader/config.go +++ b/internal/loader/config.go @@ -8,7 +8,6 @@ import ( "time" "github.com/issue9/localeutil" - "github.com/issue9/validation/is" ) // Config 配置信息,用于从文件中读取 @@ -68,7 +67,7 @@ func LoadConfig(fs fs.FS, path string) (*Config, error) { } func (conf *Config) sanitize() *FieldError { - if len(conf.URL) == 0 || !is.URL(conf.URL) { + if len(conf.URL) == 0 || !isURL(conf.URL) { return &FieldError{Message: localeutil.StringPhrase("invalid format"), Field: "url", Value: conf.URL} } diff --git a/internal/loader/is.go b/internal/loader/is.go new file mode 100644 index 0000000..a2507a2 --- /dev/null +++ b/internal/loader/is.go @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: MIT + +package loader + +import ( + "net/mail" + "net/url" +) + +func isURL(u string) bool { + _, err := url.Parse(u) + return err == nil +} + +func isEmail(u string) bool { + _, err := mail.ParseAddress(u) + return err == nil +} diff --git a/internal/loader/loader.go b/internal/loader/loader.go index 1471728..4b4fb65 100644 --- a/internal/loader/loader.go +++ b/internal/loader/loader.go @@ -12,7 +12,6 @@ import ( "path" "github.com/issue9/localeutil" - "github.com/issue9/validation/is" "golang.org/x/text/language" "golang.org/x/text/message" "gopkg.in/yaml.v3" @@ -110,15 +109,15 @@ func (author *Author) sanitize() *FieldError { return &FieldError{Field: "name", Message: Required} } - if len(author.URL) > 0 && !is.URL(author.URL) { + if len(author.URL) > 0 && !isURL(author.URL) { return &FieldError{Field: "url", Message: InvalidURL, Value: author.URL} } - if len(author.Avatar) > 0 && !is.URL(author.Avatar) { + if len(author.Avatar) > 0 && !isURL(author.Avatar) { return &FieldError{Field: "avatar", Message: InvalidURL, Value: author.Avatar} } - if len(author.Email) > 0 && !is.Email(author.Email) { + if len(author.Email) > 0 && !isEmail(author.Email) { return &FieldError{Field: "email", Message: InvalidURL, Value: author.Email} }