github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_min.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package builtin
     8  
     9  import (
    10  	"errors"
    11  	"strconv"
    12  
    13  	"github.com/wangyougui/gf/v2/text/gstr"
    14  )
    15  
    16  // RuleMin implements `min` rule:
    17  // Equal or greater than :min. It supports both integer and float.
    18  //
    19  // Format: min:min
    20  type RuleMin struct{}
    21  
    22  func init() {
    23  	Register(RuleMin{})
    24  }
    25  
    26  func (r RuleMin) Name() string {
    27  	return "min"
    28  }
    29  
    30  func (r RuleMin) Message() string {
    31  	return "The {field} value `{value}` must be equal or greater than {min}"
    32  }
    33  
    34  func (r RuleMin) Run(in RunInput) error {
    35  	var (
    36  		min, err1    = strconv.ParseFloat(in.RulePattern, 10)
    37  		valueN, err2 = strconv.ParseFloat(in.Value.String(), 10)
    38  	)
    39  	if valueN < min || err1 != nil || err2 != nil {
    40  		return errors.New(gstr.Replace(in.Message, "{min}", strconv.FormatFloat(min, 'f', -1, 64)))
    41  	}
    42  	return nil
    43  }