pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/knf/validators/validators.go (about)

     1  // Package validators provides basic KNF validators
     2  package validators
     3  
     4  // ////////////////////////////////////////////////////////////////////////////////// //
     5  //                                                                                    //
     6  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     7  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     8  //                                                                                    //
     9  // ////////////////////////////////////////////////////////////////////////////////// //
    10  
    11  import (
    12  	"fmt"
    13  	"strconv"
    14  	"strings"
    15  
    16  	"pkg.re/essentialkaos/ek.v12/knf"
    17  	"pkg.re/essentialkaos/ek.v12/strutil"
    18  )
    19  
    20  // ////////////////////////////////////////////////////////////////////////////////// //
    21  
    22  var (
    23  	// Empty returns error if config property is empty
    24  	Empty = validatorEmpty
    25  
    26  	// NotContains returns error if config property doesn't contains value from given slice
    27  	NotContains = validatorNotContains
    28  
    29  	// Less returns error if config property is less than given integer
    30  	Less = validatorLess
    31  
    32  	// Greater returns error if config property is greater than given integer
    33  	Greater = validatorGreater
    34  
    35  	// Equals returns error if config property is equals to given string
    36  	Equals = validatorEquals
    37  
    38  	// NotLen returns error if config property have wrong size
    39  	NotLen = validatorNotLen
    40  
    41  	// NotPrefix returns error if config property doesn't have given prefix
    42  	NotPrefix = validatorNotPrefix
    43  
    44  	// NotPrefix returns error if config property doesn't have given suffix
    45  	NotSuffix = validatorNotSuffix
    46  
    47  	// TypeBool returns error if config property contains non-boolean value
    48  	TypeBool = validatorTypeBool
    49  
    50  	// TypeNum returns error if config property contains non-numeric (int/uint) value
    51  	TypeNum = validatorTypeNum
    52  
    53  	// TypeNum returns error if config property contains non-float value
    54  	TypeFloat = validatorTypeFloat
    55  )
    56  
    57  // ////////////////////////////////////////////////////////////////////////////////// //
    58  
    59  func validatorEmpty(config *knf.Config, prop string, value interface{}) error {
    60  	if config.GetS(prop) == "" {
    61  		return fmt.Errorf("Property %s can't be empty", prop)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func validatorTypeBool(config *knf.Config, prop string, value interface{}) error {
    68  	propValue := config.GetS(prop)
    69  
    70  	switch strings.ToLower(propValue) {
    71  	case "", "0", "1", "true", "false", "yes", "no":
    72  		return nil
    73  	default:
    74  		return fmt.Errorf(
    75  			"Property %s contains unsupported boolean value (%s)",
    76  			prop, propValue,
    77  		)
    78  	}
    79  }
    80  
    81  func validatorTypeNum(config *knf.Config, prop string, value interface{}) error {
    82  	propValue := config.GetS(prop)
    83  
    84  	if propValue == "" {
    85  		return nil
    86  	}
    87  
    88  	_, err := strconv.Atoi(propValue)
    89  
    90  	if err != nil {
    91  		return fmt.Errorf(
    92  			"Property %s contains unsupported numeric value (%s)",
    93  			prop, propValue,
    94  		)
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  func validatorTypeFloat(config *knf.Config, prop string, value interface{}) error {
   101  	propValue := config.GetS(prop)
   102  
   103  	if propValue == "" {
   104  		return nil
   105  	}
   106  
   107  	_, err := strconv.ParseFloat(propValue, 64)
   108  
   109  	if err != nil {
   110  		return fmt.Errorf(
   111  			"Property %s contains unsupported float value (%s)",
   112  			prop, propValue,
   113  		)
   114  	}
   115  
   116  	return nil
   117  }
   118  
   119  func validatorNotContains(config *knf.Config, prop string, value interface{}) error {
   120  	switch u := value.(type) {
   121  	case []string:
   122  		currentValue := config.GetS(prop)
   123  
   124  		for _, v := range u {
   125  			if v == currentValue {
   126  				return nil
   127  			}
   128  		}
   129  
   130  		return fmt.Errorf("Property %s doesn't contains any valid value", prop)
   131  	}
   132  
   133  	return getWrongValidatorError(prop)
   134  }
   135  
   136  func validatorLess(config *knf.Config, prop string, value interface{}) error {
   137  	switch value.(type) {
   138  	case int, int32, int64, uint, uint32, uint64:
   139  		if config.GetI(prop) < value.(int) {
   140  			return fmt.Errorf("Property %s can't be less than %d", prop, value.(int))
   141  		}
   142  	case float32, float64:
   143  		if config.GetF(prop) < value.(float64) {
   144  			return fmt.Errorf("Property %s can't be less than %g", prop, value.(float64))
   145  		}
   146  	default:
   147  		return getWrongValidatorError(prop)
   148  	}
   149  
   150  	return nil
   151  }
   152  
   153  func validatorGreater(config *knf.Config, prop string, value interface{}) error {
   154  	switch value.(type) {
   155  	case int, int32, int64, uint, uint32, uint64:
   156  		if config.GetI(prop) > value.(int) {
   157  			return fmt.Errorf("Property %s can't be greater than %d", prop, value.(int))
   158  		}
   159  
   160  	case float32, float64:
   161  		if config.GetF(prop) > value.(float64) {
   162  			return fmt.Errorf("Property %s can't be greater than %g", prop, value.(float64))
   163  		}
   164  
   165  	default:
   166  		return getWrongValidatorError(prop)
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  func validatorEquals(config *knf.Config, prop string, value interface{}) error {
   173  	switch u := value.(type) {
   174  	case int, int32, int64, uint, uint32, uint64:
   175  		if config.GetI(prop) == value.(int) {
   176  			return fmt.Errorf("Property %s can't be equal %d", prop, value.(int))
   177  		}
   178  
   179  	case float32, float64:
   180  		if config.GetF(prop) == value.(float64) {
   181  			return fmt.Errorf("Property %s can't be equal %f", prop, value.(float64))
   182  		}
   183  
   184  	case bool:
   185  		if config.GetB(prop) == u {
   186  			return fmt.Errorf("Property %s can't be equal %t", prop, value.(bool))
   187  		}
   188  
   189  	case string:
   190  		if config.GetS(prop) == u {
   191  			return fmt.Errorf("Property %s can't be equal %s", prop, value.(string))
   192  		}
   193  
   194  	default:
   195  		return getWrongValidatorError(prop)
   196  	}
   197  
   198  	return nil
   199  }
   200  
   201  func validatorNotLen(config *knf.Config, prop string, value interface{}) error {
   202  	if strutil.Len(config.GetS(prop)) != value.(int) {
   203  		return fmt.Errorf("Property %s must be %d symbols long", prop, value.(int))
   204  	}
   205  
   206  	return nil
   207  }
   208  
   209  func validatorNotPrefix(config *knf.Config, prop string, value interface{}) error {
   210  	if !strings.HasPrefix(config.GetS(prop), value.(string)) {
   211  		return fmt.Errorf("Property %s must have prefix \"%s\"", prop, value.(string))
   212  	}
   213  
   214  	return nil
   215  }
   216  
   217  func validatorNotSuffix(config *knf.Config, prop string, value interface{}) error {
   218  	if !strings.HasSuffix(config.GetS(prop), value.(string)) {
   219  		return fmt.Errorf("Property %s must have suffix \"%s\"", prop, value.(string))
   220  	}
   221  
   222  	return nil
   223  }
   224  
   225  // ////////////////////////////////////////////////////////////////////////////////// //
   226  
   227  func getWrongValidatorError(prop string) error {
   228  	return fmt.Errorf("Wrong validator for property %s", prop)
   229  }