Files
gf/util/gvalid/internal/builtin/builtin_size.go
houseme 7ffdff37e4 chore: upgrade golangci-lint configuration and optimize codebase (#4236)
This PR includes the following changes:

- **Upgrade `.golangci.yml`**: Updated the configuration file to align
with the latest golangci-lint version, ensuring compatibility and
leveraging new features.
- **Refactor GitHub Action workflow**: Modified `golangci-lint.yml` in
the GitHub Actions workflow to reflect the updated configuration and
improve CI performance.
- **Codebase optimization**: Refactored code to address issues and
warnings raised by the updated golangci-lint rules, including:
  - Improved function length and complexity.
  - Enhanced error handling and variable naming conventions.
- Fixed minor issues such as unused imports and formatting
inconsistencies.

These changes aim to maintain code quality, ensure compatibility with
the latest tools, and improve overall maintainability.
2025-08-22 13:29:09 +08:00

47 lines
1.1 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package builtin
import (
"errors"
"strconv"
"strings"
"github.com/gogf/gf/v2/util/gconv"
)
// RuleSize implements `size` rule:
// Length must be :size.
// The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.
//
// Format: size:size
type RuleSize struct{}
func init() {
Register(RuleSize{})
}
func (r RuleSize) Name() string {
return "size"
}
func (r RuleSize) Message() string {
return "The {field} value `{value}` length must be {size}"
}
func (r RuleSize) Run(in RunInput) error {
var (
valueRunes = gconv.Runes(in.Value.String())
valueLen = len(valueRunes)
)
size, err := strconv.Atoi(in.RulePattern)
if valueLen != size || err != nil {
return errors.New(strings.ReplaceAll(in.Message, "{size}", strconv.Itoa(size)))
}
return nil
}