github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/inputvalidation/validators_iterable.go (about) 1 /* 2 Part of this file is copied from https://github.com/go-ozzo/ozzo-validation/blob/master/each.go. 3 Below you can find its licence. 4 5 The MIT License (MIT) 6 Copyright (c) 2016, Qiang Xue 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 9 and associated documentation files (the "Software"), to deal in the Software without restriction, 10 including without limitation the rights to use, copy, modify, merge, publish, distribute, 11 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 12 is furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in all copies or 15 substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 18 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24 package inputvalidation 25 26 import ( 27 "errors" 28 "reflect" 29 "strconv" 30 31 validation "github.com/go-ozzo/ozzo-validation/v4" 32 ) 33 34 type eachRule []validation.Rule 35 36 // Each returns a validation rule that loops through an iterable (map, slice or array) 37 // and validates each value inside with the provided rules. 38 // An empty iterable is considered valid. Use the Required rule to make sure the iterable is not empty. 39 func Each(rules ...validation.Rule) *eachRule { 40 r := eachRule(rules) 41 return &r 42 } 43 44 // Validate loops through the given iterable and calls the Ozzo Validate() method for each value. 45 func (r eachRule) Validate(value interface{}) error { 46 errs := validation.Errors{} 47 48 v := reflect.ValueOf(value) 49 50 if v.Kind() == reflect.Ptr { 51 if v.IsNil() { 52 return nil 53 } 54 v = v.Elem() 55 } 56 57 switch v.Kind() { 58 case reflect.Map: 59 for _, k := range v.MapKeys() { 60 val := r.getInterface(v.MapIndex(k)) 61 if err := validation.Validate(val, r...); err != nil { 62 errs[r.getString(k)] = err 63 } 64 } 65 case reflect.Slice, reflect.Array: 66 for i := 0; i < v.Len(); i++ { 67 val := r.getInterface(v.Index(i)) 68 if err := validation.Validate(val, r...); err != nil { 69 errs[strconv.Itoa(i)] = err 70 } 71 } 72 default: 73 return errors.New("must be an iterable (map, slice or array) or a pointer to iterable") 74 } 75 76 if len(errs) > 0 { 77 return errs 78 } 79 return nil 80 } 81 82 func (r *eachRule) getInterface(value reflect.Value) interface{} { 83 switch value.Kind() { 84 case reflect.Ptr, reflect.Interface: 85 if value.IsNil() { 86 return nil 87 } 88 return value.Elem().Interface() 89 default: 90 return value.Interface() 91 } 92 } 93 94 func (r *eachRule) getString(value reflect.Value) string { 95 switch value.Kind() { 96 case reflect.Ptr, reflect.Interface: 97 if value.IsNil() { 98 return "" 99 } 100 return value.Elem().String() 101 default: 102 return value.String() 103 } 104 }