pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/knf/validators/regexp/validators.go (about) 1 // Package regexp provides KNF validators with regular expressions 2 package regexp 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 "regexp" 14 15 "pkg.re/essentialkaos/ek.v12/knf" 16 ) 17 18 // ////////////////////////////////////////////////////////////////////////////////// // 19 20 var ( 21 // Regexp returns an error if config property does not match given regexp 22 Regexp = validateRegexp 23 ) 24 25 // ////////////////////////////////////////////////////////////////////////////////// // 26 27 func validateRegexp(config *knf.Config, prop string, value interface{}) error { 28 pattern := value.(string) 29 confVal := config.GetS(prop) 30 31 if confVal == "" || pattern == "" { 32 return nil 33 } 34 35 isMatch, err := regexp.MatchString(pattern, confVal) 36 37 if err != nil { 38 return fmt.Errorf("Can't use given regexp pattern: %v", err) 39 } 40 41 if !isMatch { 42 return fmt.Errorf("Property %s must match regexp pattern %s", prop, pattern) 43 } 44 45 return nil 46 }