github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_passport.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  // RulePassport implements `passport` rule:
    16  // Universal passport format rule:
    17  // Starting with letter, containing only numbers or underscores, length between 6 and 18
    18  //
    19  // Format: passport
    20  type RulePassport struct{}
    21  
    22  func init() {
    23  	Register(RulePassport{})
    24  }
    25  
    26  func (r RulePassport) Name() string {
    27  	return "passport"
    28  }
    29  
    30  func (r RulePassport) Message() string {
    31  	return "The {field} value `{value}` is not a valid passport format"
    32  }
    33  
    34  func (r RulePassport) Run(in RunInput) error {
    35  	ok := gregex.IsMatchString(
    36  		`^[a-zA-Z]{1}\w{5,17}$`,
    37  		in.Value.String(),
    38  	)
    39  	if ok {
    40  		return nil
    41  	}
    42  	return errors.New(in.Message)
    43  }