github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/params/validators.go (about) 1 // Copyright 2022-2023 The Inspektor Gadget authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package params 16 17 import ( 18 "fmt" 19 "net" 20 "strconv" 21 "strings" 22 "time" 23 ) 24 25 type TypeHint string 26 27 const ( 28 TypeUnknown TypeHint = "" 29 TypeBool TypeHint = "bool" 30 TypeString TypeHint = "string" 31 TypeBytes TypeHint = "bytes" 32 TypeInt TypeHint = "int" 33 TypeInt8 TypeHint = "int8" 34 TypeInt16 TypeHint = "int16" 35 TypeInt32 TypeHint = "int32" 36 TypeInt64 TypeHint = "int64" 37 TypeUint TypeHint = "uint" 38 TypeUint8 TypeHint = "uint8" 39 TypeUint16 TypeHint = "uint16" 40 TypeUint32 TypeHint = "uint32" 41 TypeUint64 TypeHint = "uint64" 42 TypeFloat32 TypeHint = "float32" 43 TypeFloat64 TypeHint = "float64" 44 TypeDuration TypeHint = "duration" 45 TypeIP TypeHint = "ip" 46 ) 47 48 var typeHintValidators = map[TypeHint]ParamValidator{ 49 TypeBool: ValidateBool, 50 TypeInt: ValidateInt(strconv.IntSize), 51 TypeInt8: ValidateInt(8), 52 TypeInt16: ValidateInt(16), 53 TypeInt32: ValidateInt(32), 54 TypeInt64: ValidateInt(64), 55 TypeUint: ValidateUint(strconv.IntSize), 56 TypeUint8: ValidateUint(8), 57 TypeUint16: ValidateUint(16), 58 TypeUint32: ValidateUint(32), 59 TypeUint64: ValidateUint(64), 60 TypeFloat32: ValidateFloat(32), 61 TypeFloat64: ValidateFloat(64), 62 TypeDuration: ValidateDuration, 63 TypeIP: ValidateIP, 64 } 65 66 type ValueHint string 67 68 type ParamValidator func(value string) error 69 70 func ValidateInt(bitsize int) func(string) error { 71 return func(value string) error { 72 _, err := strconv.ParseInt(value, 10, bitsize) 73 if err != nil { 74 return fmt.Errorf("expected numeric value: %w", err) 75 } 76 return nil 77 } 78 } 79 80 func ValidateUint(bitsize int) func(string) error { 81 return func(value string) error { 82 _, err := strconv.ParseUint(value, 10, bitsize) 83 if err != nil { 84 return fmt.Errorf("expected numeric value: %w", err) 85 } 86 return nil 87 } 88 } 89 90 func ValidateFloat(bitsize int) func(string) error { 91 return func(value string) error { 92 _, err := strconv.ParseFloat(value, bitsize) 93 if err != nil { 94 return fmt.Errorf("expected numeric value: %w", err) 95 } 96 return nil 97 } 98 } 99 100 func ValidateBool(value string) error { 101 value = strings.ToLower(value) 102 if value != "true" && value != "false" { 103 return fmt.Errorf("expected 'true' or 'false', got: %q", value) 104 } 105 return nil 106 } 107 108 func ValidateIntRange(min, max int64) func(value string) error { 109 return func(value string) error { 110 number, err := strconv.ParseInt(value, 10, 64) 111 if err != nil { 112 return fmt.Errorf("expected numeric value") 113 } 114 if number < min || number > max { 115 return fmt.Errorf("number out of range: got %d, expected min %d, max %d", number, min, max) 116 } 117 return nil 118 } 119 } 120 121 func ValidateUintRange(min, max uint64) func(value string) error { 122 return func(value string) error { 123 number, err := strconv.ParseUint(value, 10, 64) 124 if err != nil { 125 return fmt.Errorf("expected numeric value: %w", err) 126 } 127 if number < min || number > max { 128 return fmt.Errorf("number out of range: got %d, expected min %d, max %d", number, min, max) 129 } 130 return nil 131 } 132 } 133 134 func ValidateSlice(validator ParamValidator) func(value string) error { 135 return func(value string) error { 136 // No value to validate 137 if len(value) == 0 { 138 return nil 139 } 140 for i, val := range strings.Split(value, ",") { 141 if err := validator(val); err != nil { 142 return fmt.Errorf("entry #%d (%q): %w", i+1, val, err) 143 } 144 } 145 return nil 146 } 147 } 148 149 func ValidateDuration(value string) error { 150 _, err := time.ParseDuration(value) 151 return err 152 } 153 154 func ValidateIP(value string) error { 155 if value != "" && net.ParseIP(value) == nil { 156 return fmt.Errorf("%q is not a valid IP address", value) 157 } 158 return nil 159 }