/
builtin_min_length.go
46 lines (38 loc) · 1.15 KB
/
builtin_min_length.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 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"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)
// RuleMinLength implements `min-length` rule:
// Length is equal or greater than :min.
// The length is calculated using unicode string, which means one chinese character or letter both has the length of 1.
//
// Format: min-length:min
type RuleMinLength struct{}
func init() {
Register(RuleMinLength{})
}
func (r RuleMinLength) Name() string {
return "min-length"
}
func (r RuleMinLength) Message() string {
return "The {field} value `{value}` length must be equal or greater than {min}"
}
func (r RuleMinLength) Run(in RunInput) error {
var (
valueRunes = gconv.Runes(in.Value.String())
valueLen = len(valueRunes)
)
min, err := strconv.Atoi(in.RulePattern)
if valueLen < min || err != nil {
return errors.New(gstr.Replace(in.Message, "{min}", strconv.Itoa(min)))
}
return nil
}