github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_url.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  
    12  	"github.com/gogf/gf/v2/text/gregex"
    13  )
    14  
    15  // RuleUrl implements `url` rule:
    16  // URL.
    17  //
    18  // Format: url
    19  type RuleUrl struct{}
    20  
    21  func init() {
    22  	Register(RuleUrl{})
    23  }
    24  
    25  func (r RuleUrl) Name() string {
    26  	return "url"
    27  }
    28  
    29  func (r RuleUrl) Message() string {
    30  	return "The {field} value `{value}` is not a valid URL address"
    31  }
    32  
    33  func (r RuleUrl) Run(in RunInput) error {
    34  	ok := gregex.IsMatchString(
    35  		`(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]`,
    36  		in.Value.String(),
    37  	)
    38  	if ok {
    39  		return nil
    40  	}
    41  	return errors.New(in.Message)
    42  }