42
vendor/github.com/go-openapi/validate/values.go
generated
vendored
42
vendor/github.com/go-openapi/validate/values.go
generated
vendored
@@ -17,6 +17,7 @@ package validate
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/go-openapi/errors"
|
||||
@@ -26,11 +27,17 @@ import (
|
||||
|
||||
// Enum validates if the data is a member of the enum
|
||||
func Enum(path, in string, data interface{}, enum interface{}) *errors.Validation {
|
||||
return EnumCase(path, in, data, enum, true)
|
||||
}
|
||||
|
||||
// EnumCase validates if the data is a member of the enum and may respect case-sensitivity for strings
|
||||
func EnumCase(path, in string, data interface{}, enum interface{}, caseSensitive bool) *errors.Validation {
|
||||
val := reflect.ValueOf(enum)
|
||||
if val.Kind() != reflect.Slice {
|
||||
return nil
|
||||
}
|
||||
|
||||
dataString := convertEnumCaseStringKind(data, caseSensitive)
|
||||
var values []interface{}
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
ele := val.Index(i)
|
||||
@@ -39,6 +46,10 @@ func Enum(path, in string, data interface{}, enum interface{}) *errors.Validatio
|
||||
if reflect.DeepEqual(data, enumValue) {
|
||||
return nil
|
||||
}
|
||||
enumString := convertEnumCaseStringKind(enumValue, caseSensitive)
|
||||
if dataString != nil && enumString != nil && strings.EqualFold(*dataString, *enumString) {
|
||||
return nil
|
||||
}
|
||||
actualType := reflect.TypeOf(enumValue)
|
||||
if actualType == nil { // Safeguard. Frankly, I don't know how we may get a nil
|
||||
continue
|
||||
@@ -56,6 +67,21 @@ func Enum(path, in string, data interface{}, enum interface{}) *errors.Validatio
|
||||
return errors.EnumFail(path, in, data, values)
|
||||
}
|
||||
|
||||
// convertEnumCaseStringKind converts interface if it is kind of string and case insensitivity is set
|
||||
func convertEnumCaseStringKind(value interface{}, caseSensitive bool) *string {
|
||||
if caseSensitive {
|
||||
return nil
|
||||
}
|
||||
|
||||
val := reflect.ValueOf(value)
|
||||
if val.Kind() != reflect.String {
|
||||
return nil
|
||||
}
|
||||
|
||||
str := fmt.Sprintf("%v", value)
|
||||
return &str
|
||||
}
|
||||
|
||||
// MinItems validates that there are at least n items in a slice
|
||||
func MinItems(path, in string, size, min int64) *errors.Validation {
|
||||
if size < min {
|
||||
@@ -361,26 +387,26 @@ func IsValueValidAgainstRange(val interface{}, typeName, format, prefix, path st
|
||||
var errVal error
|
||||
|
||||
switch typeName {
|
||||
case "integer":
|
||||
case integerType:
|
||||
switch format {
|
||||
case "int32":
|
||||
case integerFormatInt32:
|
||||
_, errVal = swag.ConvertInt32(stringRep)
|
||||
case "uint32":
|
||||
case integerFormatUInt32:
|
||||
_, errVal = swag.ConvertUint32(stringRep)
|
||||
case "uint64":
|
||||
case integerFormatUInt64:
|
||||
_, errVal = swag.ConvertUint64(stringRep)
|
||||
case "int64":
|
||||
case integerFormatInt64:
|
||||
fallthrough
|
||||
default:
|
||||
_, errVal = swag.ConvertInt64(stringRep)
|
||||
}
|
||||
case "number":
|
||||
case numberType:
|
||||
fallthrough
|
||||
default:
|
||||
switch format {
|
||||
case "float", "float32":
|
||||
case numberFormatFloat, numberFormatFloat32:
|
||||
_, errVal = swag.ConvertFloat32(stringRep)
|
||||
case "double", "float64":
|
||||
case numberFormatDouble, numberFormatFloat64:
|
||||
fallthrough
|
||||
default:
|
||||
// No check can be performed here since
|
||||
|
||||
Reference in New Issue
Block a user