github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/config/validate.go (about)

     1  // MIT License
     2  
     3  // Copyright (c) 2020 Tree Xie
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    21  // SOFTWARE.
    22  
    23  package config
    24  
    25  import (
    26  	"net/url"
    27  	"reflect"
    28  	"regexp"
    29  	"strings"
    30  	"time"
    31  
    32  	"github.com/dustin/go-humanize"
    33  	"github.com/go-playground/validator/v10"
    34  	us "github.com/vicanso/upstream"
    35  )
    36  
    37  var defaultValidator = validator.New()
    38  
    39  func init() {
    40  
    41  	addAlias("xName", "max=20")
    42  	addValidate("xDuration", func(fl validator.FieldLevel) bool {
    43  		value, ok := toString(fl)
    44  		if !ok {
    45  			return false
    46  		}
    47  		_, err := time.ParseDuration(value)
    48  		return err == nil
    49  	})
    50  	addValidate("xAddr", func(fl validator.FieldLevel) bool {
    51  		value, ok := toString(fl)
    52  		if !ok {
    53  			return false
    54  		}
    55  		urlInfo, err := url.Parse(value)
    56  		if err != nil {
    57  			return false
    58  		}
    59  		return contains([]string{"http", "https"}, urlInfo.Scheme)
    60  	})
    61  	addValidate("xURLPath", func(fl validator.FieldLevel) bool {
    62  		value, ok := toString(fl)
    63  		if !ok {
    64  			return false
    65  		}
    66  		return value != "" && value[0] == '/'
    67  	})
    68  	addValidate("xDivide", func(fl validator.FieldLevel) bool {
    69  		value, ok := toString(fl)
    70  		if !ok {
    71  			return false
    72  		}
    73  		arr := strings.Split(value, ":")
    74  		return len(arr) == 2
    75  	})
    76  	addValidate("xSize", func(fl validator.FieldLevel) bool {
    77  		value, ok := toString(fl)
    78  		if !ok {
    79  			return false
    80  		}
    81  		_, err := humanize.ParseBytes(value)
    82  		return err == nil
    83  	})
    84  	addValidate("xFilter", func(fl validator.FieldLevel) bool {
    85  		value, ok := toString(fl)
    86  		if !ok {
    87  			return false
    88  		}
    89  		if len(value) > 1000 {
    90  			return false
    91  		}
    92  		_, err := regexp.Compile(value)
    93  		return err == nil
    94  	})
    95  	addValidate("xPolicy", func(fl validator.FieldLevel) bool {
    96  		value, ok := toString(fl)
    97  		if !ok {
    98  			return false
    99  		}
   100  		return contains([]string{
   101  			us.PolicyFirst,
   102  			us.PolicyRandom,
   103  			us.PolicyRoundRobin,
   104  			us.PolicyLeastconn,
   105  		}, value)
   106  	})
   107  }
   108  
   109  // toString 转换为string
   110  func toString(fl validator.FieldLevel) (string, bool) {
   111  	value := fl.Field()
   112  	if value.Kind() != reflect.String {
   113  		return "", false
   114  	}
   115  	return value.String(), true
   116  }
   117  
   118  func contains(arr []string, str string) bool {
   119  	found := false
   120  	for _, item := range arr {
   121  		if item == str {
   122  			found = true
   123  			break
   124  		}
   125  	}
   126  	return found
   127  }
   128  
   129  func addValidate(tag string, fn validator.Func, args ...bool) {
   130  	err := defaultValidator.RegisterValidation(tag, fn, args...)
   131  	if err != nil {
   132  		panic(err)
   133  	}
   134  }
   135  
   136  func addAlias(alias, tags string) {
   137  	defaultValidator.RegisterAlias(alias, tags)
   138  }