Update a few dependencies

github.com/go-openapi/*
github.com/asaskevich/govalidator
This commit is contained in:
Mikhail Mazurskiy
2018-10-18 23:33:10 +11:00
parent b8731a76f0
commit 8763223ab9
191 changed files with 18648 additions and 1800 deletions

View File

@@ -15,8 +15,8 @@
package validate
import (
"fmt"
"reflect"
"regexp"
"unicode/utf8"
"github.com/go-openapi/errors"
@@ -40,7 +40,7 @@ func Enum(path, in string, data interface{}, enum interface{}) *errors.Validatio
return nil
}
actualType := reflect.TypeOf(enumValue)
if actualType == nil {
if actualType == nil { // Safeguard. Frankly, I don't know how we may get a nil
continue
}
expectedValue := reflect.ValueOf(data)
@@ -139,7 +139,10 @@ func RequiredNumber(path, in string, data float64) *errors.Validation {
// Pattern validates a string against a regular expression
func Pattern(path, in, data, pattern string) *errors.Validation {
re := regexp.MustCompile(pattern)
re, err := compileRegexp(pattern)
if err != nil {
return errors.FailedPattern(path, in, fmt.Sprintf("%s, but pattern is invalid: %s", pattern, err.Error()))
}
if !re.MatchString(data) {
return errors.FailedPattern(path, in, pattern)
}
@@ -196,9 +199,15 @@ func MinimumUint(path, in string, data, min uint64, exclusive bool) *errors.Vali
// MultipleOf validates if the provided number is a multiple of the factor
func MultipleOf(path, in string, data, factor float64) *errors.Validation {
mult := data / factor
// multipleOf factor must be positive
if factor < 0 {
return errors.MultipleOfMustBePositive(path, in, factor)
}
var mult float64
if factor < 1 {
mult = 1 / factor * data
} else {
mult = data / factor
}
if !swag.IsFloat64AJSONInteger(mult) {
return errors.NotMultipleOf(path, in, factor)
@@ -206,6 +215,28 @@ func MultipleOf(path, in string, data, factor float64) *errors.Validation {
return nil
}
// MultipleOfInt validates if the provided integer is a multiple of the factor
func MultipleOfInt(path, in string, data int64, factor int64) *errors.Validation {
// multipleOf factor must be positive
if factor < 0 {
return errors.MultipleOfMustBePositive(path, in, factor)
}
mult := data / factor
if mult*factor != data {
return errors.NotMultipleOf(path, in, factor)
}
return nil
}
// MultipleOfUint validates if the provided unsigned integer is a multiple of the factor
func MultipleOfUint(path, in string, data, factor uint64) *errors.Validation {
mult := data / factor
if mult*factor != data {
return errors.NotMultipleOf(path, in, factor)
}
return nil
}
// FormatOf validates if a string matches a format in the format registry
func FormatOf(path, in, format, data string, registry strfmt.Registry) *errors.Validation {
if registry == nil {
@@ -217,6 +248,151 @@ func FormatOf(path, in, format, data string, registry strfmt.Registry) *errors.V
if ok := registry.Validates(format, data); !ok {
return errors.InvalidType(path, in, format, data)
}
return nil
}
// MaximumNativeType provides native type constraint validation as a facade
// to various numeric types versions of Maximum constraint check.
//
// Assumes that any possible loss conversion during conversion has been
// checked beforehand.
//
// NOTE: currently, the max value is marshalled as a float64, no matter what,
// which means there may be a loss during conversions (e.g. for very large integers)
//
// TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
func MaximumNativeType(path, in string, val interface{}, max float64, exclusive bool) *errors.Validation {
kind := reflect.ValueOf(val).Type().Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value := valueHelp.asInt64(val)
return MaximumInt(path, in, value, int64(max), exclusive)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value := valueHelp.asUint64(val)
if max < 0 {
return errors.ExceedsMaximum(path, in, max, exclusive)
}
return MaximumUint(path, in, value, uint64(max), exclusive)
case reflect.Float32, reflect.Float64:
fallthrough
default:
value := valueHelp.asFloat64(val)
return Maximum(path, in, value, max, exclusive)
}
}
// MinimumNativeType provides native type constraint validation as a facade
// to various numeric types versions of Minimum constraint check.
//
// Assumes that any possible loss conversion during conversion has been
// checked beforehand.
//
// NOTE: currently, the min value is marshalled as a float64, no matter what,
// which means there may be a loss during conversions (e.g. for very large integers)
//
// TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
func MinimumNativeType(path, in string, val interface{}, min float64, exclusive bool) *errors.Validation {
kind := reflect.ValueOf(val).Type().Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value := valueHelp.asInt64(val)
return MinimumInt(path, in, value, int64(min), exclusive)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value := valueHelp.asUint64(val)
if min < 0 {
return nil
}
return MinimumUint(path, in, value, uint64(min), exclusive)
case reflect.Float32, reflect.Float64:
fallthrough
default:
value := valueHelp.asFloat64(val)
return Minimum(path, in, value, min, exclusive)
}
}
// MultipleOfNativeType provides native type constraint validation as a facade
// to various numeric types version of MultipleOf constraint check.
//
// Assumes that any possible loss conversion during conversion has been
// checked beforehand.
//
// NOTE: currently, the multipleOf factor is marshalled as a float64, no matter what,
// which means there may be a loss during conversions (e.g. for very large integers)
//
// TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free
func MultipleOfNativeType(path, in string, val interface{}, multipleOf float64) *errors.Validation {
kind := reflect.ValueOf(val).Type().Kind()
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
value := valueHelp.asInt64(val)
return MultipleOfInt(path, in, value, int64(multipleOf))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
value := valueHelp.asUint64(val)
return MultipleOfUint(path, in, value, uint64(multipleOf))
case reflect.Float32, reflect.Float64:
fallthrough
default:
value := valueHelp.asFloat64(val)
return MultipleOf(path, in, value, multipleOf)
}
}
// IsValueValidAgainstRange checks that a numeric value is compatible with
// the range defined by Type and Format, that is, may be converted without loss.
//
// NOTE: this check is about type capacity and not formal verification such as: 1.0 != 1L
func IsValueValidAgainstRange(val interface{}, typeName, format, prefix, path string) error {
kind := reflect.ValueOf(val).Type().Kind()
// What is the string representation of val
stringRep := ""
switch kind {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
stringRep = swag.FormatUint64(valueHelp.asUint64(val))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
stringRep = swag.FormatInt64(valueHelp.asInt64(val))
case reflect.Float32, reflect.Float64:
stringRep = swag.FormatFloat64(valueHelp.asFloat64(val))
default:
return fmt.Errorf("%s value number range checking called with invalid (non numeric) val type in %s", prefix, path)
}
var errVal error
switch typeName {
case "integer":
switch format {
case "int32":
_, errVal = swag.ConvertInt32(stringRep)
case "uint32":
_, errVal = swag.ConvertUint32(stringRep)
case "uint64":
_, errVal = swag.ConvertUint64(stringRep)
case "int64":
fallthrough
default:
_, errVal = swag.ConvertInt64(stringRep)
}
case "number":
fallthrough
default:
switch format {
case "float", "float32":
_, errVal = swag.ConvertFloat32(stringRep)
case "double", "float64":
fallthrough
default:
// No check can be performed here since
// no number beyond float64 is supported
}
}
if errVal != nil { // We don't report the actual errVal from strconv
if format != "" {
errVal = fmt.Errorf("%s value must be of type %s with format %s in %s", prefix, typeName, format, path)
} else {
errVal = fmt.Errorf("%s value must be of type %s (default format) in %s", prefix, typeName, path)
}
}
return errVal
}