github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_not_in.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  	"strings"
    12  
    13  	"github.com/gogf/gf/v2/text/gstr"
    14  )
    15  
    16  // RuleNotIn implements `not-in` rule:
    17  // Value should not be in: value1,value2,...
    18  //
    19  // Format: not-in:value1,value2,...
    20  type RuleNotIn struct{}
    21  
    22  func init() {
    23  	Register(RuleNotIn{})
    24  }
    25  
    26  func (r RuleNotIn) Name() string {
    27  	return "not-in"
    28  }
    29  
    30  func (r RuleNotIn) Message() string {
    31  	return "The {field} value `{value}` must not be in range: {pattern}"
    32  }
    33  
    34  func (r RuleNotIn) Run(in RunInput) error {
    35  	var (
    36  		ok    = true
    37  		value = in.Value.String()
    38  	)
    39  	for _, rulePattern := range gstr.SplitAndTrim(in.RulePattern, ",") {
    40  		if in.Option.CaseInsensitive {
    41  			ok = !strings.EqualFold(value, strings.TrimSpace(rulePattern))
    42  		} else {
    43  			ok = strings.Compare(value, strings.TrimSpace(rulePattern)) != 0
    44  		}
    45  		if !ok {
    46  			return errors.New(in.Message)
    47  		}
    48  	}
    49  	return nil
    50  }