github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_integer.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/gogf/gf.
     6  
     7  package builtin
     8  
     9  import (
    10  	"errors"
    11  	"strconv"
    12  )
    13  
    14  // RuleInteger implements `integer` rule:
    15  // Integer.
    16  //
    17  // Format: integer
    18  type RuleInteger struct{}
    19  
    20  func init() {
    21  	Register(RuleInteger{})
    22  }
    23  
    24  func (r RuleInteger) Name() string {
    25  	return "integer"
    26  }
    27  
    28  func (r RuleInteger) Message() string {
    29  	return "The {field} value `{value}` is not an integer"
    30  }
    31  
    32  func (r RuleInteger) Run(in RunInput) error {
    33  	if _, err := strconv.Atoi(in.Value.String()); err == nil {
    34  		return nil
    35  	}
    36  	return errors.New(in.Message)
    37  }