-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexample_enum_test.go
84 lines (71 loc) · 1.84 KB
/
example_enum_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
package jsonschema_test
import (
"fmt"
"github.com/swaggest/assertjson"
"github.com/swaggest/jsonschema-go"
)
type WeekDay string
func (WeekDay) Enum() []interface{} {
return []interface{}{
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
}
}
type Shop struct {
Days []WeekDay `json:"days,omitempty"` // This property uses dedicated named type to express enum.
Days2 []string `json:"days2,omitempty"` // This property uses schema preparer to set up enum.
// This scalar property uses field tag to set up enum.
Day string `json:"day" enum:"Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"`
}
var _ jsonschema.Preparer = Shop{}
func (Shop) PrepareJSONSchema(schema *jsonschema.Schema) error {
schema.Properties["days2"].TypeObject.WithEnum(
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
)
return nil
}
func ExampleEnum() {
reflector := jsonschema.Reflector{}
s, err := reflector.Reflect(Shop{}, jsonschema.StripDefinitionNamePrefix("JsonschemaGoTest"))
if err != nil {
panic(err)
}
j, err := assertjson.MarshalIndentCompact(s, "", " ", 80)
if err != nil {
panic(err)
}
fmt.Println(string(j))
// Output:
// {
// "definitions":{
// "WeekDay":{
// "enum":["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
// "type":"string"
// }
// },
// "properties":{
// "day":{
// "enum":["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
// "type":"string"
// },
// "days":{"items":{"$ref":"#/definitions/WeekDay"},"type":"array"},
// "days2":{
// "items":{"type":"string"},
// "enum":["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
// "type":"array"
// }
// },
// "type":"object"
// }
}