update vendor/ dir to latest w/o heroku, moby

had to lock a lot of things in place
This commit is contained in:
Reed Allman
2017-08-03 02:38:15 -07:00
parent 780791da1c
commit 30f3c45dbc
5637 changed files with 191713 additions and 1133103 deletions

View File

@@ -0,0 +1,53 @@
package validate
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/go-openapi/spec"
"github.com/go-openapi/strfmt"
)
var defaulterFixturesPath = filepath.Join("fixtures", "defaulting")
func TestDefaulter(t *testing.T) {
fname := filepath.Join(defaulterFixturesPath, "schema.json")
b, err := ioutil.ReadFile(fname)
assert.NoError(t, err)
var schema spec.Schema
assert.NoError(t, json.Unmarshal(b, &schema))
err = spec.ExpandSchema(&schema, nil, nil /*new(noopResCache)*/)
assert.NoError(t, err, fname+" should expand cleanly")
validator := NewSchemaValidator(&schema, nil, "", strfmt.Default)
x := map[string]interface{}{
"nested": map[string]interface{}{},
"all": map[string]interface{}{},
"any": map[string]interface{}{},
"one": map[string]interface{}{},
}
t.Logf("Before: %v", x)
r := validator.Validate(x)
assert.False(t, r.HasErrors(), fmt.Sprintf("unexpected validation error: %v", r.AsError()))
r.ApplyDefaults()
t.Logf("After: %v", x)
var expected interface{}
err = json.Unmarshal([]byte(`{
"int": 42,
"str": "Hello",
"obj": {"foo": "bar"},
"nested": {"inner": 7},
"all": {"foo": 42, "bar": 42},
"any": {"foo": 42},
"one": {"bar": 42}
}`), &expected)
assert.NoError(t, err)
assert.Equal(t, expected, x)
}

View File

@@ -0,0 +1,101 @@
{
"properties": {
"int": {
"type": "integer",
"default": 42
},
"str": {
"type": "string",
"minLength": 4,
"default": "Hello"
},
"obj": {
"type": "object",
"default": {"foo": "bar"}
},
"nested": {
"type": "object",
"properties": {
"inner": {
"type": "integer",
"default": 7
}
}
},
"all": {
"allOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "integer",
"default": 42
}
}
},
{
"type": "object",
"properties": {
"bar": {
"type": "integer",
"default": 42
}
}
}
]
},
"any": {
"anyOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "integer",
"default": 42
}
}
},
{
"type": "object",
"properties": {
"bar": {
"type": "integer",
"default": 42
}
}
}
]
},
"one": {
"oneOf": [
{
"type": "object",
"properties": {
"foo": {
"type": "integer"
}
},
"required": ["foo"]
},
{
"type": "object",
"properties": {
"bar": {
"type": "integer",
"default": 42
}
}
}
]
},
"not": {
"not": {
"type": "object",
"default": {
"foo": 1
}
}
}
},
"required": ["int", "str", "nested", "all", "any", "one"]
}

View File

@@ -64,14 +64,6 @@ func (o *objectValidator) Validate(data interface{}) *Result {
}
res := new(Result)
if len(o.Required) > 0 {
for _, k := range o.Required {
if _, ok := val[k]; !ok {
res.AddErrors(errors.Required(o.Path+"."+k, o.In))
continue
}
}
}
if o.AdditionalProperties != nil && !o.AdditionalProperties.Allows {
for k := range val {
@@ -102,6 +94,8 @@ func (o *objectValidator) Validate(data interface{}) *Result {
}
}
createdFromDefaults := map[string]bool{}
for pName, pSchema := range o.Properties {
rName := pName
if o.Path != "" {
@@ -109,7 +103,24 @@ func (o *objectValidator) Validate(data interface{}) *Result {
}
if v, ok := val[pName]; ok {
res.Merge(NewSchemaValidator(&pSchema, o.Root, rName, o.KnownFormats).Validate(v))
r := NewSchemaValidator(&pSchema, o.Root, rName, o.KnownFormats).Validate(v)
res.Merge(r)
} else if pSchema.Default != nil {
createdFromDefaults[pName] = true
pName := pName // shaddow
def := pSchema.Default
res.Defaulters = append(res.Defaulters, DefaulterFunc(func() {
val[pName] = def
}))
}
}
if len(o.Required) > 0 {
for _, k := range o.Required {
if _, ok := val[k]; !ok && !createdFromDefaults[k] {
res.AddErrors(errors.Required(o.Path+"."+k, o.In))
continue
}
}
}
@@ -140,9 +151,6 @@ func (o *objectValidator) validatePatternProperty(key string, value interface{},
res := validator.Validate(value)
result.Merge(res)
if res.IsValid() {
succeededOnce = true
}
}
}

View File

@@ -25,10 +25,21 @@ var (
Debug = os.Getenv("SWAGGER_DEBUG") != ""
)
type Defaulter interface {
Apply()
}
type DefaulterFunc func()
func (f DefaulterFunc) Apply() {
f()
}
// Result represents a validation result
type Result struct {
Errors []error
MatchCount int
Defaulters []Defaulter
}
// Merge merges this result with the other one, preserving match counts etc
@@ -38,6 +49,7 @@ func (r *Result) Merge(other *Result) *Result {
}
r.AddErrors(other.Errors...)
r.MatchCount += other.MatchCount
r.Defaulters = append(r.Defaulters, other.Defaulters...)
return r
}
@@ -69,3 +81,9 @@ func (r *Result) AsError() error {
}
return errors.CompositeValidationError(r.Errors...)
}
func (r *Result) ApplyDefaults() {
for _, d := range r.Defaulters {
d.Apply()
}
}

View File

@@ -148,7 +148,6 @@ func (s *SchemaValidator) commonValidator() valueValidator {
return &basicCommonValidator{
Path: s.Path,
In: s.in,
Default: s.Schema.Default,
Enum: s.Schema.Enum,
}
}
@@ -184,7 +183,6 @@ func (s *SchemaValidator) stringValidator() valueValidator {
return &stringValidator{
Path: s.Path,
In: s.in,
Default: s.Schema.Default,
MaxLength: s.Schema.MaxLength,
MinLength: s.Schema.MinLength,
Pattern: s.Schema.Pattern,
@@ -195,7 +193,6 @@ func (s *SchemaValidator) formatValidator() valueValidator {
return &formatValidator{
Path: s.Path,
In: s.in,
//Default: s.Schema.Default,
Format: s.Schema.Format,
KnownFormats: s.KnownFormats,
}

View File

@@ -89,6 +89,7 @@ func (s *schemaPropsValidator) Applies(source interface{}, kind reflect.Kind) bo
func (s *schemaPropsValidator) Validate(data interface{}) *Result {
mainResult := new(Result)
var firstSuccess *Result
if len(s.anyOfValidators) > 0 {
var bestFailures *Result
succeededOnce := false
@@ -97,6 +98,9 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
if result.IsValid() {
bestFailures = nil
succeededOnce = true
if firstSuccess == nil {
firstSuccess = result
}
break
}
if bestFailures == nil || result.MatchCount > bestFailures.MatchCount {
@@ -109,11 +113,14 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
}
if bestFailures != nil {
mainResult.Merge(bestFailures)
} else if firstSuccess != nil {
mainResult.Merge(firstSuccess)
}
}
if len(s.oneOfValidators) > 0 {
var bestFailures *Result
var firstSuccess *Result
validated := 0
for _, oneOfSchema := range s.oneOfValidators {
@@ -121,6 +128,9 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
if result.IsValid() {
validated++
bestFailures = nil
if firstSuccess == nil {
firstSuccess = result
}
continue
}
if validated == 0 && (bestFailures == nil || result.MatchCount > bestFailures.MatchCount) {
@@ -133,6 +143,8 @@ func (s *schemaPropsValidator) Validate(data interface{}) *Result {
if bestFailures != nil {
mainResult.Merge(bestFailures)
}
} else if firstSuccess != nil {
mainResult.Merge(firstSuccess)
}
}