github.com/wangyougui/gf/v2@v2.6.5/util/gvalid/internal/builtin/builtin_array.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/internal/json"
    13  )
    14  
    15  // RuleArray implements `array` rule:
    16  // Value should be type of array.
    17  //
    18  // Format: array
    19  type RuleArray struct{}
    20  
    21  func init() {
    22  	Register(RuleArray{})
    23  }
    24  
    25  func (r RuleArray) Name() string {
    26  	return "array"
    27  }
    28  
    29  func (r RuleArray) Message() string {
    30  	return "The {field} value `{value}` is not of valid array type"
    31  }
    32  
    33  func (r RuleArray) Run(in RunInput) error {
    34  	if in.Value.IsSlice() {
    35  		return nil
    36  	}
    37  	if json.Valid(in.Value.Bytes()) {
    38  		value := in.Value.String()
    39  		if len(value) > 1 && value[0] == '[' && value[len(value)-1] == ']' {
    40  			return nil
    41  		}
    42  	}
    43  	return errors.New(in.Message)
    44  }