-
Notifications
You must be signed in to change notification settings - Fork 3
/
schema_test.go
126 lines (104 loc) · 3.47 KB
/
schema_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
123
124
125
126
package oas
import (
"regexp"
"testing"
"github.com/go-courier/ptr"
)
func TestSchema(t *testing.T) {
g := NewCaseGroup("Schema")
g.It("empty", `{}`, Schema{})
g.It("integer", `{"type":"integer","format":"int32"}`, Integer())
g.It("long", `{"type":"integer","format":"int64"}`, Long())
g.It("float", `{"type":"number","format":"float"}`, Float())
g.It("double", `{"type":"number","format":"double"}`, Double())
g.It(
"string",
`{"title":"title","type":"string","description":"desc"}`,
String().WithTitle("title").WithDesc("desc"),
)
g.It("byte", `{"type":"string","format":"byte"}`, Byte())
g.It("binary", `{"type":"string","format":"binary"}`, Binary())
g.It("date", `{"type":"string","format":"date"}`, Date())
g.It("date-time", `{"type":"string","format":"date-time"}`, DateTime())
g.It("password", `{"type":"string","format":"password"}`, Password())
g.It("boolean", `{"type":"boolean"}`, Boolean())
g.It("array", `{"type":"array","items":{"type":"string"}}`, ItemsOf(String()))
g.It("object",
`{"type":"object","properties":{"key1":{"type":"string"},"key2":{"type":"string"}},"required":["key1"]}`,
ObjectOf(
Props{
"key1": String(),
"key2": String(),
},
"key1",
))
g.It("object with additional",
`{"type":"object","additionalProperties":{"type":"string"}}`,
MapOf(String()),
)
g.It("object with additional and pror",
`{"type":"object","additionalProperties":{"type":"string"},"propertyNames":{"type":"string"}}`,
KeyValueOf(String(), String()),
)
g.It("anyOf", `{"anyOf":[{"type":"string"},{"type":"boolean"}]}`, AnyOf(
String(),
Boolean(),
))
g.It("oneOf", `{"oneOf":[{"type":"string"},{"type":"boolean"}]}`, OneOf(
String(),
Boolean(),
))
g.It("allOf", `{"allOf":[{"type":"string"},{"type":"boolean"}]}`, AllOf(
String(),
Boolean(),
))
g.It("not", `{"not":{"type":"string"}}`, Not(String()))
validation := &SchemaValidation{
MultipleOf: ptr.Float64(2),
Maximum: ptr.Float64(10),
ExclusiveMaximum: true,
Minimum: ptr.Float64(1),
ExclusiveMinimum: true,
MaxLength: ptr.Uint64(10),
MinLength: ptr.Uint64(0),
Pattern: regexp.MustCompile("/+d/").String(),
MaxItems: ptr.Uint64(10),
MinItems: ptr.Uint64(1),
UniqueItems: true,
MaxProperties: ptr.Uint64(10),
MinProperties: ptr.Uint64(1),
Required: []string{"key"},
Enum: []interface{}{"1", "2", "3"},
}
g.It(
"with string validation",
`{"type":"string","maxLength":10,"minLength":0,"pattern":"/+d/","enum":["1","2","3"]}`,
String().WithValidation(validation),
)
g.It(
"with integer validation",
`{"type":"integer","format":"int32","multipleOf":2,"maximum":10,"exclusiveMaximum":true,"minimum":1,"exclusiveMinimum":true,"enum":["1","2","3"]}`,
Integer().WithValidation(validation),
)
g.It(
"with number validation",
`{"type":"number","format":"float","multipleOf":2,"maximum":10,"exclusiveMaximum":true,"minimum":1,"exclusiveMinimum":true,"enum":["1","2","3"]}`,
Float().WithValidation(validation),
)
g.It(
"with array validation",
`{"type":"array","items":{"type":"string"},"maxItems":10,"minItems":1,"uniqueItems":true,"enum":["1","2","3"]}`,
ItemsOf(String()).WithValidation(validation),
)
g.It("object",
`{"type":"object","properties":{"key1":{"type":"string"},"key2":{"type":"string"}},"maxProperties":10,"minProperties":1,"required":["key"],"enum":["1","2","3"]}`,
ObjectOf(
Props{
"key1": String(),
"key2": String(),
},
"key1",
).WithValidation(validation),
)
g.Run(t)
}