-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathschema_test.go
More file actions
224 lines (191 loc) · 5.35 KB
/
Copy pathschema_test.go
File metadata and controls
224 lines (191 loc) · 5.35 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package jsonschema
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSchemaValidateTypeNames(t *testing.T) {
var err error
toSchema := func(s string) *Schema {
return &Schema{
Properties: map[string]*Schema{
"foo": {
Type: Type(s),
},
},
}
}
err = toSchema("string").validate()
assert.NoError(t, err)
err = toSchema("boolean").validate()
assert.NoError(t, err)
err = toSchema("number").validate()
assert.NoError(t, err)
err = toSchema("integer").validate()
assert.NoError(t, err)
err = toSchema("int").validate()
assert.EqualError(t, err, "type int is not a recognized json schema type. Please use \"integer\" instead")
err = toSchema("float").validate()
assert.EqualError(t, err, "type float is not a recognized json schema type. Please use \"number\" instead")
err = toSchema("bool").validate()
assert.EqualError(t, err, "type bool is not a recognized json schema type. Please use \"boolean\" instead")
err = toSchema("foobar").validate()
assert.EqualError(t, err, "type foobar is not a recognized json schema type")
}
func TestSchemaLoadIntegers(t *testing.T) {
schema, err := Load("./testdata/schema-load-int/schema-valid.json")
assert.NoError(t, err)
assert.Equal(t, int64(1), schema.Properties["abc"].Default)
assert.Equal(t, []any{int64(1), int64(2), int64(3)}, schema.Properties["abc"].Enum)
}
func TestSchemaLoadIntegersWithInvalidDefault(t *testing.T) {
_, err := Load("./testdata/schema-load-int/schema-invalid-default.json")
assert.EqualError(t, err, "failed to parse default value for property abc: expected integer value, got: 1.1")
}
func TestSchemaLoadIntegersWithInvalidEnums(t *testing.T) {
_, err := Load("./testdata/schema-load-int/schema-invalid-enum.json")
assert.EqualError(t, err, "failed to parse enum value 2.4 at index 1 for property abc: expected integer value, got: 2.4")
}
func TestSchemaValidateDefaultType(t *testing.T) {
invalidSchema := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "number",
Default: "abc",
},
},
}
err := invalidSchema.validate()
assert.EqualError(t, err, "type validation for default value of property foo failed: expected type float, but value is \"abc\"")
validSchema := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "boolean",
Default: true,
},
},
}
err = validSchema.validate()
assert.NoError(t, err)
}
func TestSchemaValidateEnumType(t *testing.T) {
invalidSchema := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "boolean",
Enum: []any{true, "false"},
},
},
}
err := invalidSchema.validate()
assert.EqualError(t, err, "type validation for enum at index 1 failed for property foo: expected type boolean, but value is \"false\"")
validSchema := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "boolean",
Enum: []any{true, false},
},
},
}
err = validSchema.validate()
assert.NoError(t, err)
}
func TestSchemaValidateErrorWhenDefaultValueIsNotInEnums(t *testing.T) {
invalidSchema := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
Default: "abc",
Enum: []any{"def", "ghi"},
},
},
}
err := invalidSchema.validate()
assert.EqualError(t, err, "list of enum values for property foo does not contain default value abc: [def ghi]")
validSchema := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
Default: "abc",
Enum: []any{"def", "ghi", "abc"},
},
},
}
err = validSchema.validate()
assert.NoError(t, err)
}
func TestSchemaValidatePatternType(t *testing.T) {
s := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "number",
Pattern: "abc",
},
},
}
assert.EqualError(t, s.validate(), "property \"foo\" has a non-empty regex pattern \"abc\" specified. Patterns are only supported for string properties")
s = &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
Pattern: "abc",
},
},
}
assert.NoError(t, s.validate())
}
func TestSchemaValidateIncorrectRegex(t *testing.T) {
s := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
// invalid regex, missing the closing brace
Pattern: "(abc",
},
},
}
assert.EqualError(t, s.validate(), "invalid regex pattern \"(abc\" provided for property \"foo\": error parsing regexp: missing closing ): `(abc`")
}
func TestSchemaValidatePatternDefault(t *testing.T) {
s := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
Pattern: "abc",
Default: "def",
},
},
}
assert.EqualError(t, s.validate(), "default value \"def\" for property \"foo\" does not match specified regex pattern: \"abc\"")
s = &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
Pattern: "a.*d",
Default: "axyzd",
},
},
}
assert.NoError(t, s.validate())
}
func TestSchemaValidatePatternEnum(t *testing.T) {
s := &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
Pattern: "a.*c",
Enum: []any{"abc", "def", "abbc"},
},
},
}
assert.EqualError(t, s.validate(), "enum value \"def\" at index 1 for property \"foo\" does not match specified regex pattern: \"a.*c\"")
s = &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
Pattern: "a.*d",
Enum: []any{"abd", "axybgd", "abbd"},
},
},
}
assert.NoError(t, s.validate())
}