gopkg.in/essentialkaos/ek.v3@v3.5.1/knf/validators.go (about) 1 package knf 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2009-2016 Essential Kaos // 6 // Essential Kaos Open Source License <http://essentialkaos.com/ekol?en> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 ) 13 14 // ////////////////////////////////////////////////////////////////////////////////// // 15 16 // PropertyValidator is default type of property validation func 17 type PropertyValidator func(config *Config, prop string, value interface{}) error 18 19 // ////////////////////////////////////////////////////////////////////////////////// // 20 21 // Empty check if given config property is empty or not 22 var Empty = func(config *Config, prop string, value interface{}) error { 23 if config.GetS(prop) == "" { 24 return fmt.Errorf("Property %s can't be empty", prop) 25 } 26 27 return nil 28 } 29 30 // Less check if given config property is less then defined value or not 31 var Less = func(config *Config, prop string, value interface{}) error { 32 switch value.(type) { 33 case int, int32, int64, uint, uint32, uint64: 34 if config.GetI(prop) < value.(int) { 35 return fmt.Errorf("Property %s can't be less than %d", prop, value.(int)) 36 } 37 case float32, float64: 38 if config.GetF(prop) < value.(float64) { 39 return fmt.Errorf("Property %s can't be less than %g", prop, value.(float64)) 40 } 41 default: 42 return getWrongValidatorError(prop) 43 } 44 45 return nil 46 } 47 48 // Greater check if given config property is greater then defined value or not 49 var Greater = func(config *Config, prop string, value interface{}) error { 50 switch value.(type) { 51 case int, int32, int64, uint, uint32, uint64: 52 if config.GetI(prop) > value.(int) { 53 return fmt.Errorf("Property %s can't be greater than %d", prop, value.(int)) 54 } 55 56 case float32, float64: 57 if config.GetF(prop) > value.(float64) { 58 return fmt.Errorf("Property %s can't be greater than %g", prop, value.(float64)) 59 } 60 61 default: 62 return getWrongValidatorError(prop) 63 } 64 65 return nil 66 } 67 68 // Equals check if given config property is equals then defined value or not 69 var Equals = func(config *Config, prop string, value interface{}) error { 70 switch value.(type) { 71 case int, int32, int64, uint, uint32, uint64: 72 if config.GetI(prop) == value.(int) { 73 return fmt.Errorf("Property %s can't be equal %d", prop, value.(int)) 74 } 75 76 case float32, float64: 77 if config.GetF(prop) == value.(float64) { 78 return fmt.Errorf("Property %s can't be equal %f", prop, value.(float64)) 79 } 80 81 case bool: 82 if config.GetB(prop) == value.(bool) { 83 return fmt.Errorf("Property %s can't be equal %t", prop, value.(bool)) 84 } 85 86 case string: 87 if config.GetS(prop) == value.(string) { 88 return fmt.Errorf("Property %s can't be equal %s", prop, value.(string)) 89 } 90 91 default: 92 return getWrongValidatorError(prop) 93 } 94 95 return nil 96 } 97 98 // ////////////////////////////////////////////////////////////////////////////////// // 99 100 func getWrongValidatorError(prop string) error { 101 return fmt.Errorf("Wrong validator for property %s", prop) 102 }