diff --git a/internal/collections/infra/mysql_repo.go b/internal/collections/infra/mysql_repo.go index 2eed1502..5b1f82c9 100644 --- a/internal/collections/infra/mysql_repo.go +++ b/internal/collections/infra/mysql_repo.go @@ -308,11 +308,7 @@ func (r mysqlRepo) reCountSubjectTags(ctx context.Context, tx *query.Query, var countMap = make(map[string]uint32) for _, tag := range tagList { - if len(tag.Tag.Name) == 0 { - continue - } - - if dam.ZeroWithPattern.MatchString(tag.Tag.Name) { + if !dam.ValidateTag(tag.Tag.Name) { continue } diff --git a/internal/pkg/dam/dam.go b/internal/pkg/dam/dam.go index 6191fb02..ac525dab 100644 --- a/internal/pkg/dam/dam.go +++ b/internal/pkg/dam/dam.go @@ -97,4 +97,21 @@ func AllPrintableChar(text string) bool { return true } -var ZeroWithPattern = regexp.MustCompile(`\p{Cf}|\p{Cc}|\p{Co}`) +var ZeroWidthPattern = regexp.MustCompile(`\p{Cf}|\p{Cc}|\p{Co}`) +var ExtraSpacePattern = regexp.MustCompile("[\u3000 ]") + +func ValidateTag(t string) bool { + if len(t) == 0 { + return false + } + + if !AllPrintableChar(t) { + return false + } + + if ExtraSpacePattern.MatchString(t) { + return false + } + + return true +} diff --git a/web/req/collection.go b/web/req/collection.go index 8e419369..c2c6e360 100644 --- a/web/req/collection.go +++ b/web/req/collection.go @@ -15,11 +15,10 @@ package req import ( + "fmt" "strings" "unicode/utf8" - "github.com/samber/lo" - "github.com/bangumi/server/internal/collections/domain/collection" "github.com/bangumi/server/internal/pkg/dam" "github.com/bangumi/server/internal/pkg/null" @@ -47,15 +46,9 @@ func (v *SubjectEpisodeCollectionPatch) Validate() error { } } - if len(v.Tags) > 0 { - if !lo.EveryBy(v.Tags, dam.AllPrintableChar) { - return res.BadRequest("invisible character are included in tags") - } - - if lo.ContainsBy(v.Tags, func(item string) bool { - return len(item) == 0 - }) { - return res.BadRequest("zero length tags are included in tags") + for _, tag := range v.Tags { + if !dam.ValidateTag(tag) { + return res.BadRequest(fmt.Sprintf("invalid tag: %q", tag)) } }