| 1 |
// Copyright 2015 go-swagger maintainers |
| 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 validate |
| 16 |
|
| 17 |
import ( |
| 18 |
"reflect" |
| 19 |
|
| 20 |
"k8s.io/kube-openapi/pkg/validation/errors" |
| 21 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 22 |
) |
| 23 |
|
| 24 |
// ValueValidator validates the values it applies to. |
| 25 |
type ValueValidator interface { |
| 26 |
// SetPath sets the exact path of the validator prior to calling Validate. |
| 27 |
// The exact path contains the map keys and array indices to locate the |
| 28 |
// value to be validated from the root data element. |
| 29 |
SetPath(path string) |
| 30 |
// Applies returns true if the validator applies to the valueKind |
| 31 |
// from source. Validate will be called if and only if Applies returns true. |
| 32 |
Applies(source interface{}, valueKind reflect.Kind) bool |
| 33 |
// Validate validates the value. |
| 34 |
Validate(value interface{}) *Result |
| 35 |
} |
| 36 |
|
| 37 |
type basicCommonValidator struct { |
| 38 |
Path string |
| 39 |
In string |
| 40 |
Default interface{} |
| 41 |
Enum []interface{} |
| 42 |
} |
| 43 |
|
| 44 |
func (b *basicCommonValidator) SetPath(path string) { |
| 45 |
b.Path = path |
| 46 |
} |
| 47 |
|
| 48 |
func (b *basicCommonValidator) Applies(source interface{}, kind reflect.Kind) bool { |
| 49 |
switch source.(type) { |
| 50 |
case *spec.Schema: |
| 51 |
return true |
| 52 |
} |
| 53 |
return false |
| 54 |
} |
| 55 |
|
| 56 |
func (b *basicCommonValidator) Validate(data interface{}) (res *Result) { |
| 57 |
if len(b.Enum) > 0 { |
| 58 |
for _, enumValue := range b.Enum { |
| 59 |
actualType := reflect.TypeOf(enumValue) |
| 60 |
if actualType != nil { // Safeguard |
| 61 |
expectedValue := reflect.ValueOf(data) |
| 62 |
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) { |
| 63 |
if reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), enumValue) { |
| 64 |
return nil |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
return errorHelp.sErr(errors.EnumFail(b.Path, b.In, data, b.Enum)) |
| 70 |
} |
| 71 |
return nil |
| 72 |
} |
| 73 |
|
| 74 |
// numberValidator validates numeric values against constraints and standard formats. |
| 75 |
// |
| 76 |
// It is responsible for checking: |
| 77 |
// - MultipleOf: The value must be a multiple of the given factor. |
| 78 |
// - Maximum/ExclusiveMaximum: The value must be less than or equal to (or strictly less than) the maximum. |
| 79 |
// - Minimum/ExclusiveMinimum: The value must be greater than or equal to (or strictly greater than) the minimum. |
| 80 |
// - Numeric Format Enforcement: |
| 81 |
// - int32 and float (float32): The validator verifies that the value fits within the |
| 82 |
// range of the specified type. If a value overflows (e.g., an int64 provided for an |
| 83 |
// int32 field), a validation error is returned. |
| 84 |
// - double (float64): Values exceeding the maximum representable float64 (e.g., 1e500) |
| 85 |
// fail during the initial parsing phase with an "invalid type conversion" error, |
| 86 |
// before this validator is reached. |
| 87 |
type numberValidator struct { |
| 88 |
Path string |
| 89 |
In string |
| 90 |
Default interface{} |
| 91 |
MultipleOf *float64 |
| 92 |
Maximum *float64 |
| 93 |
ExclusiveMaximum bool |
| 94 |
Minimum *float64 |
| 95 |
ExclusiveMinimum bool |
| 96 |
// Allows for more accurate behavior regarding integers |
| 97 |
Type string |
| 98 |
Format string |
| 99 |
} |
| 100 |
|
| 101 |
func (n *numberValidator) SetPath(path string) { |
| 102 |
n.Path = path |
| 103 |
} |
| 104 |
|
| 105 |
func (n *numberValidator) Applies(source interface{}, kind reflect.Kind) bool { |
| 106 |
switch source.(type) { |
| 107 |
case *spec.Schema: |
| 108 |
isInt := kind >= reflect.Int && kind <= reflect.Uint64 |
| 109 |
isFloat := kind == reflect.Float32 || kind == reflect.Float64 |
| 110 |
r := isInt || isFloat |
| 111 |
debugLog("schema props validator for %q applies %t for %T (kind: %v) isInt=%t, isFloat=%t\n", n.Path, r, source, kind, isInt, isFloat) |
| 112 |
return r |
| 113 |
} |
| 114 |
debugLog("schema props validator for %q applies %t for %T (kind: %v)\n", n.Path, false, source, kind) |
| 115 |
return false |
| 116 |
} |
| 117 |
|
| 118 |
// Validate provides a validator for generic JSON numbers, |
| 119 |
// |
| 120 |
// By default, numbers are internally represented as float64. |
| 121 |
// Formats float, or float32 may alter this behavior by mapping to float32. |
| 122 |
// A special validation process is followed for integers, with optional "format": |
| 123 |
// this is an attempt to provide a validation with native types. |
| 124 |
// |
| 125 |
// NOTE: since the constraint specified (boundary, multipleOf) is unmarshalled |
| 126 |
// as float64, loss of information remains possible (e.g. on very large integers). |
| 127 |
// |
| 128 |
// Since this value directly comes from the unmarshalling, it is not possible |
| 129 |
// at this stage of processing to check further and guarantee the correctness of such values. |
| 130 |
// |
| 131 |
// Normally, the JSON Number.MAX_SAFE_INTEGER (resp. Number.MIN_SAFE_INTEGER) |
| 132 |
// would check we do not get such a loss. |
| 133 |
// |
| 134 |
// If this is the case, replace AddErrors() by AddWarnings() and IsValid() by !HasWarnings(). |
| 135 |
// |
| 136 |
// TODO: consider replacing boundary check errors by simple warnings. |
| 137 |
// |
| 138 |
// TODO: default boundaries with MAX_SAFE_INTEGER are not checked (specific to json.Number?) |
| 139 |
func (n *numberValidator) Validate(val interface{}) *Result { |
| 140 |
res := new(Result) |
| 141 |
|
| 142 |
resMultiple := new(Result) |
| 143 |
resMinimum := new(Result) |
| 144 |
resMaximum := new(Result) |
| 145 |
|
| 146 |
// Used only to attempt to validate constraint on value, |
| 147 |
// even though value or constraint specified do not match type and format |
| 148 |
data := valueHelp.asFloat64(val) |
| 149 |
|
| 150 |
// Is the provided value within the range of the specified numeric type and format? |
| 151 |
res.AddErrors(IsValueValidAgainstRange(val, n.Type, n.Format, "Checked", n.Path)) |
| 152 |
|
| 153 |
// nolint: dupl |
| 154 |
if n.MultipleOf != nil { |
| 155 |
// Is the constraint specifier within the range of the specific numeric type and format? |
| 156 |
resMultiple.AddErrors(IsValueValidAgainstRange(*n.MultipleOf, n.Type, n.Format, "MultipleOf", n.Path)) |
| 157 |
if resMultiple.IsValid() { |
| 158 |
// Constraint validated with compatible types |
| 159 |
if err := MultipleOfNativeType(n.Path, n.In, val, *n.MultipleOf); err != nil { |
| 160 |
resMultiple.Merge(errorHelp.sErr(err)) |
| 161 |
} |
| 162 |
} else { |
| 163 |
// Constraint nevertheless validated, converted as general number |
| 164 |
if err := MultipleOf(n.Path, n.In, data, *n.MultipleOf); err != nil { |
| 165 |
resMultiple.Merge(errorHelp.sErr(err)) |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
// nolint: dupl |
| 171 |
if n.Maximum != nil { |
| 172 |
// Is the constraint specifier within the range of the specific numeric type and format? |
| 173 |
resMaximum.AddErrors(IsValueValidAgainstRange(*n.Maximum, n.Type, n.Format, "Maximum boundary", n.Path)) |
| 174 |
if resMaximum.IsValid() { |
| 175 |
// Constraint validated with compatible types |
| 176 |
if err := MaximumNativeType(n.Path, n.In, val, *n.Maximum, n.ExclusiveMaximum); err != nil { |
| 177 |
resMaximum.Merge(errorHelp.sErr(err)) |
| 178 |
} |
| 179 |
} else { |
| 180 |
// Constraint nevertheless validated, converted as general number |
| 181 |
if err := Maximum(n.Path, n.In, data, *n.Maximum, n.ExclusiveMaximum); err != nil { |
| 182 |
resMaximum.Merge(errorHelp.sErr(err)) |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// nolint: dupl |
| 188 |
if n.Minimum != nil { |
| 189 |
// Is the constraint specifier within the range of the specific numeric type and format? |
| 190 |
resMinimum.AddErrors(IsValueValidAgainstRange(*n.Minimum, n.Type, n.Format, "Minimum boundary", n.Path)) |
| 191 |
if resMinimum.IsValid() { |
| 192 |
// Constraint validated with compatible types |
| 193 |
if err := MinimumNativeType(n.Path, n.In, val, *n.Minimum, n.ExclusiveMinimum); err != nil { |
| 194 |
resMinimum.Merge(errorHelp.sErr(err)) |
| 195 |
} |
| 196 |
} else { |
| 197 |
// Constraint nevertheless validated, converted as general number |
| 198 |
if err := Minimum(n.Path, n.In, data, *n.Minimum, n.ExclusiveMinimum); err != nil { |
| 199 |
resMinimum.Merge(errorHelp.sErr(err)) |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
res.Merge(resMultiple, resMinimum, resMaximum) |
| 204 |
res.Inc() |
| 205 |
return res |
| 206 |
} |
| 207 |
|
| 208 |
type stringValidator struct { |
| 209 |
MaxLength *int64 |
| 210 |
MinLength *int64 |
| 211 |
Pattern string |
| 212 |
Path string |
| 213 |
In string |
| 214 |
} |
| 215 |
|
| 216 |
func (s *stringValidator) SetPath(path string) { |
| 217 |
s.Path = path |
| 218 |
} |
| 219 |
|
| 220 |
func (s *stringValidator) Applies(source interface{}, kind reflect.Kind) bool { |
| 221 |
switch source.(type) { |
| 222 |
case *spec.Schema: |
| 223 |
r := kind == reflect.String |
| 224 |
debugLog("string validator for %q applies %t for %T (kind: %v)\n", s.Path, r, source, kind) |
| 225 |
return r |
| 226 |
} |
| 227 |
debugLog("string validator for %q applies %t for %T (kind: %v)\n", s.Path, false, source, kind) |
| 228 |
return false |
| 229 |
} |
| 230 |
|
| 231 |
func (s *stringValidator) Validate(val interface{}) *Result { |
| 232 |
data, ok := val.(string) |
| 233 |
if !ok { |
| 234 |
return errorHelp.sErr(errors.InvalidType(s.Path, s.In, stringType, val)) |
| 235 |
} |
| 236 |
|
| 237 |
if s.MaxLength != nil { |
| 238 |
if err := MaxLength(s.Path, s.In, data, *s.MaxLength); err != nil { |
| 239 |
return errorHelp.sErr(err) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if s.MinLength != nil { |
| 244 |
if err := MinLength(s.Path, s.In, data, *s.MinLength); err != nil { |
| 245 |
return errorHelp.sErr(err) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if s.Pattern != "" { |
| 250 |
if err := Pattern(s.Path, s.In, data, s.Pattern); err != nil { |
| 251 |
return errorHelp.sErr(err) |
| 252 |
} |
| 253 |
} |
| 254 |
return nil |
| 255 |
} |