github.com/yandex/pandora@v0.5.32/core/config/validations.go (about)

     1  package config
     2  
     3  import (
     4  	"net"
     5  	"regexp"
     6  	"time"
     7  
     8  	"github.com/asaskevich/govalidator"
     9  	"github.com/c2h5oh/datasize"
    10  	validator "gopkg.in/bluesuncorp/validator.v9"
    11  )
    12  
    13  func MinTimeValidation(fl validator.FieldLevel) bool {
    14  	t, min, ok := getTimeForValidation(fl.Field().Interface(), fl.Param())
    15  	return ok && min <= t
    16  }
    17  func MaxTimeValidation(fl validator.FieldLevel) bool {
    18  	t, max, ok := getTimeForValidation(fl.Field().Interface(), fl.Param())
    19  	return ok && t <= max
    20  }
    21  
    22  func getTimeForValidation(v interface{}, param string) (actual time.Duration, check time.Duration, ok bool) {
    23  	check, err := time.ParseDuration(param)
    24  	if err != nil {
    25  		return
    26  	}
    27  	actual, ok = v.(time.Duration)
    28  	return
    29  }
    30  
    31  func MinSizeValidation(fl validator.FieldLevel) bool {
    32  	t, min, ok := getSizeForValidation(fl.Field().Interface(), fl.Param())
    33  	return ok && min <= t
    34  }
    35  func MaxSizeValidation(fl validator.FieldLevel) bool {
    36  	t, max, ok := getSizeForValidation(fl.Field().Interface(), fl.Param())
    37  	return ok && t <= max
    38  }
    39  
    40  func getSizeForValidation(v interface{}, param string) (actual, check datasize.ByteSize, ok bool) {
    41  	err := check.UnmarshalText([]byte(param))
    42  	if err != nil {
    43  		return
    44  	}
    45  	actual, ok = v.(datasize.ByteSize)
    46  	return
    47  }
    48  
    49  // "host:port" or ":port"
    50  func EndpointStringValidation(value string) bool {
    51  	host, port, err := net.SplitHostPort(value)
    52  	return err == nil &&
    53  		(host == "" || govalidator.IsHost(host)) &&
    54  		govalidator.IsPort(port)
    55  }
    56  
    57  // pathRegexp is regexp for at least one path component.
    58  // Valid characters are taken from RFC 3986.
    59  var pathRegexp = regexp.MustCompile(`^(/[a-zA-Z0-9._~!$&'()*+,;=:@%-]+)+$`)
    60  
    61  func URLPathStringValidation(value string) bool {
    62  	return pathRegexp.MatchString(value)
    63  }