forked from go-openapi/spec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validations_test.go
122 lines (101 loc) · 2.95 KB
/
validations_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package spec
import (
"testing"
"github.com/go-openapi/swag"
"github.com/stretchr/testify/require"
)
func mkVal() SchemaValidations {
return SchemaValidations{
CommonValidations: CommonValidations{
Maximum: swag.Float64(2.5),
ExclusiveMaximum: true,
Minimum: swag.Float64(3.4),
ExclusiveMinimum: true,
MaxLength: swag.Int64(15),
MinLength: swag.Int64(16),
Pattern: "abc",
MaxItems: swag.Int64(17),
MinItems: swag.Int64(18),
UniqueItems: true,
MultipleOf: swag.Float64(4.4),
Enum: []interface{}{"a", 12.5},
},
PatternProperties: SchemaProperties{
"x": *BooleanProperty(),
"y": *BooleanProperty(),
},
MinProperties: swag.Int64(19),
MaxProperties: swag.Int64(20),
}
}
func TestValidations(t *testing.T) {
var cv CommonValidations
val := mkVal()
cv.SetValidations(val)
expectedCV := val.CommonValidations
require.EqualValues(t, expectedCV, cv)
require.True(t, cv.HasArrayValidations())
require.True(t, cv.HasNumberValidations())
require.True(t, cv.HasStringValidations())
require.True(t, cv.HasEnum())
cv.Enum = nil
require.False(t, cv.HasEnum())
cv.MaxLength = nil
require.True(t, cv.HasStringValidations())
cv.MinLength = nil
require.True(t, cv.HasStringValidations())
cv.Pattern = ""
require.False(t, cv.HasStringValidations())
cv.Minimum = nil
require.True(t, cv.HasNumberValidations())
cv.Maximum = nil
require.True(t, cv.HasNumberValidations())
cv.MultipleOf = nil
require.False(t, cv.HasNumberValidations())
cv.MaxItems = nil
require.True(t, cv.HasArrayValidations())
cv.MinItems = nil
require.True(t, cv.HasArrayValidations())
cv.UniqueItems = false
require.False(t, cv.HasArrayValidations())
val = mkVal()
expectedSV := val
expectedSV.PatternProperties = nil
expectedSV.MinProperties = nil
expectedSV.MaxProperties = nil
val = mkVal()
cv.SetValidations(val)
require.EqualValues(t, expectedSV, cv.Validations())
var sv SchemaValidations
val = mkVal()
sv.SetValidations(val)
expectedSV = val
require.EqualValues(t, expectedSV, sv)
require.EqualValues(t, val, sv.Validations())
require.True(t, sv.HasObjectValidations())
sv.MinProperties = nil
require.True(t, sv.HasObjectValidations())
sv.MaxProperties = nil
require.True(t, sv.HasObjectValidations())
sv.PatternProperties = nil
require.False(t, sv.HasObjectValidations())
val = mkVal()
cv.SetValidations(val)
cv.ClearStringValidations()
require.False(t, cv.HasStringValidations())
cv.ClearNumberValidations()
require.False(t, cv.HasNumberValidations())
cv.ClearArrayValidations()
require.False(t, cv.HasArrayValidations())
sv.SetValidations(val)
sv.ClearObjectValidations(func(validation string, _ interface{}) {
switch validation {
case "minProperties", "maxProperties", "patternProperties":
return
default:
t.Logf("unexpected validation %s", validation)
t.Fail()
}
})
require.Falsef(t, sv.HasObjectValidations(), "%#v", sv)
}