github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_ip.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  
    12  	"github.com/wangyougui/gf/v2/net/gipv4"
    13  	"github.com/wangyougui/gf/v2/net/gipv6"
    14  )
    15  
    16  // RuleIp implements `ip` rule:
    17  // IPv4/IPv6.
    18  //
    19  // Format: ip
    20  type RuleIp struct{}
    21  
    22  func init() {
    23  	Register(RuleIp{})
    24  }
    25  
    26  func (r RuleIp) Name() string {
    27  	return "ip"
    28  }
    29  
    30  func (r RuleIp) Message() string {
    31  	return "The {field} value `{value}` is not a valid IP address"
    32  }
    33  
    34  func (r RuleIp) Run(in RunInput) error {
    35  	var (
    36  		ok    bool
    37  		value = in.Value.String()
    38  	)
    39  	if ok = gipv4.Validate(value) || gipv6.Validate(value); !ok {
    40  		return errors.New(in.Message)
    41  	}
    42  	return nil
    43  }