Skip to content

Commit e931a23

Browse files
authored
Merge pull request #14 from go-openapi/fix-enum-validation
fix int enum validation
2 parents 03d947b + 39be12f commit e931a23

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

fixtures/schemas/int-enum.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"schema": {
3+
"type": "object",
4+
"properties": {
5+
"sizes": {
6+
"type": "object",
7+
"properties": {
8+
"first": {
9+
"type": "object",
10+
"properties": {
11+
"count": {
12+
"type": "integer",
13+
"default": 3,
14+
"enum": [3, 5, 7]
15+
}
16+
},
17+
"required": ["count"]
18+
}
19+
},
20+
"required": ["first"]
21+
}
22+
},
23+
"required": ["sizes"]
24+
},
25+
"valid": {
26+
"sizes": {
27+
"first": {
28+
"count": 3
29+
}
30+
}
31+
},
32+
"invalid": {
33+
"sizes": {
34+
"first": {
35+
"count": 2
36+
}
37+
}
38+
}
39+
}

type_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ type schemaTestT struct {
3939
}
4040
}
4141

42+
type schemasTestT struct {
43+
Schema *spec.Schema `json:"schema"`
44+
Valid interface{} `json:"valid"`
45+
Invalid interface{} `json:"invalid"`
46+
}
47+
4248
var jsonSchemaFixturesPath = filepath.Join("fixtures", "jsonschema_suite")
49+
var schemaFixturesPath = filepath.Join("fixtures", "schemas")
4350

4451
var ints = []interface{}{
4552
1,
@@ -164,3 +171,41 @@ func TestJSONSchemaSuite(t *testing.T) {
164171
}
165172
}
166173
}
174+
175+
func TestSchemaFixtures(t *testing.T) {
176+
files, err := ioutil.ReadDir(schemaFixturesPath)
177+
if err != nil {
178+
t.Fatal(err)
179+
}
180+
181+
for _, f := range files {
182+
if f.IsDir() {
183+
continue
184+
}
185+
fileName := f.Name()
186+
specName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
187+
188+
t.Log("Running " + specName)
189+
b, _ := ioutil.ReadFile(filepath.Join(schemaFixturesPath, fileName))
190+
191+
var testDescriptions []schemasTestT
192+
json.Unmarshal(b, &testDescriptions)
193+
194+
for _, testDescription := range testDescriptions {
195+
196+
err := spec.ExpandSchema(testDescription.Schema, nil, nil /*new(noopResCache)*/)
197+
if assert.NoError(t, err) {
198+
199+
validator := NewSchemaValidator(testDescription.Schema, nil, "data", strfmt.Default)
200+
valid := validator.Validate(testDescription.Valid)
201+
if assert.NotNil(t, valid, specName+" should validate") {
202+
assert.Empty(t, valid.Errors, specName+".valid should not have errors")
203+
}
204+
invalid := validator.Validate(testDescription.Invalid)
205+
if assert.NotNil(t, invalid, specName+" should validate") {
206+
assert.NotEmpty(t, invalid.Errors, specName+".invalid should have errors")
207+
}
208+
}
209+
}
210+
}
211+
}

validator.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,15 @@ func (b *basicCommonValidator) Applies(source interface{}, kind reflect.Kind) bo
156156
func (b *basicCommonValidator) Validate(data interface{}) (res *Result) {
157157
if len(b.Enum) > 0 {
158158
for _, enumValue := range b.Enum {
159-
if data != nil && reflect.DeepEqual(enumValue, data) {
160-
return nil
159+
actualType := reflect.TypeOf(enumValue)
160+
if actualType == nil {
161+
continue
162+
}
163+
expectedValue := reflect.ValueOf(data)
164+
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
165+
if reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), enumValue) {
166+
return nil
167+
}
161168
}
162169
}
163170
return sErr(errors.EnumFail(b.Path, b.In, data, b.Enum))

0 commit comments

Comments
 (0)