Add Basic Enumerations (#55)
* Added the ability to specify basic enumerations through tags.
* Added data on enums to the readme.
Matt Mc authored 4 years ago
GitHub committed 4 years ago
8 | 8 |
|
9 | 9 |
- Supports arbitrarily complex types, including `interface{}`, maps, slices, etc.
|
10 | 10 |
- Supports json-schema features such as minLength, maxLength, pattern, format, etc.
|
|
11 |
- Supports simple string and numeric enums.
|
11 | 12 |
- Supports custom property fields via the `jsonschema_extras` struct tag.
|
12 | 13 |
|
13 | 14 |
## Example
|
|
23 | 24 |
BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"`
|
24 | 25 |
YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"`
|
25 | 26 |
Metadata interface{} `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"`
|
|
27 |
FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
|
26 | 28 |
}
|
27 | 29 |
```
|
28 | 30 |
|
|
84 | 86 |
},
|
85 | 87 |
"a": "b",
|
86 | 88 |
"foo": "bar"
|
|
89 |
},
|
|
90 |
"fav_color": {
|
|
91 |
"type": "string",
|
|
92 |
"enum": [
|
|
93 |
"red",
|
|
94 |
"green",
|
|
95 |
"blue"
|
|
96 |
]
|
87 | 97 |
}
|
88 | 98 |
},
|
89 | 99 |
"additionalProperties": false,
|
25 | 25 |
"TestFlag",
|
26 | 26 |
"age",
|
27 | 27 |
"email",
|
28 | |
"Baz"
|
|
28 |
"Baz",
|
|
29 |
"color"
|
29 | 30 |
],
|
30 | 31 |
"properties": {
|
31 | 32 |
"some_base_property": {
|
|
122 | 123 |
"type": "string",
|
123 | 124 |
"foo": "bar",
|
124 | 125 |
"hello": "world"
|
|
126 |
},
|
|
127 |
"color": {
|
|
128 |
"enum": [
|
|
129 |
"red",
|
|
130 |
"green",
|
|
131 |
"blue"
|
|
132 |
],
|
|
133 |
"type": "string"
|
|
134 |
},
|
|
135 |
"rank": {
|
|
136 |
"enum": [
|
|
137 |
1,
|
|
138 |
2,
|
|
139 |
3
|
|
140 |
],
|
|
141 |
"type": "integer"
|
|
142 |
},
|
|
143 |
"mult": {
|
|
144 |
"enum": [
|
|
145 |
1,
|
|
146 |
1.5,
|
|
147 |
2
|
|
148 |
],
|
|
149 |
"type": "number"
|
125 | 150 |
}
|
126 | 151 |
},
|
127 | 152 |
"additionalProperties": true,
|
25 | 25 |
"TestFlag",
|
26 | 26 |
"age",
|
27 | 27 |
"email",
|
28 | |
"Baz"
|
|
28 |
"Baz",
|
|
29 |
"color"
|
29 | 30 |
],
|
30 | 31 |
"properties": {
|
31 | 32 |
"some_base_property": {
|
|
122 | 123 |
"type": "string",
|
123 | 124 |
"foo": "bar",
|
124 | 125 |
"hello": "world"
|
|
126 |
},
|
|
127 |
"color": {
|
|
128 |
"enum": [
|
|
129 |
"red",
|
|
130 |
"green",
|
|
131 |
"blue"
|
|
132 |
],
|
|
133 |
"type": "string"
|
|
134 |
},
|
|
135 |
"rank": {
|
|
136 |
"enum": [
|
|
137 |
1,
|
|
138 |
2,
|
|
139 |
3
|
|
140 |
],
|
|
141 |
"type": "integer"
|
|
142 |
},
|
|
143 |
"mult": {
|
|
144 |
"enum": [
|
|
145 |
1,
|
|
146 |
1.5,
|
|
147 |
2
|
|
148 |
],
|
|
149 |
"type": "number"
|
125 | 150 |
}
|
126 | 151 |
},
|
127 | 152 |
"additionalProperties": false,
|
10 | 10 |
"TestFlag",
|
11 | 11 |
"age",
|
12 | 12 |
"email",
|
13 | |
"Baz"
|
|
13 |
"Baz",
|
|
14 |
"color"
|
14 | 15 |
],
|
15 | 16 |
"properties": {
|
16 | 17 |
"some_base_property": {
|
|
107 | 108 |
"type": "string",
|
108 | 109 |
"foo": "bar",
|
109 | 110 |
"hello": "world"
|
|
111 |
},
|
|
112 |
"color": {
|
|
113 |
"enum": [
|
|
114 |
"red",
|
|
115 |
"green",
|
|
116 |
"blue"
|
|
117 |
],
|
|
118 |
"type": "string"
|
|
119 |
},
|
|
120 |
"rank": {
|
|
121 |
"enum": [
|
|
122 |
1,
|
|
123 |
2,
|
|
124 |
3
|
|
125 |
],
|
|
126 |
"type": "integer"
|
|
127 |
},
|
|
128 |
"mult": {
|
|
129 |
"enum": [
|
|
130 |
1.0,
|
|
131 |
1.5,
|
|
132 |
2.0
|
|
133 |
],
|
|
134 |
"type": "number"
|
110 | 135 |
}
|
111 | 136 |
},
|
112 | 137 |
"additionalProperties": false,
|
18 | 18 |
"TestFlag",
|
19 | 19 |
"age",
|
20 | 20 |
"email",
|
21 | |
"Baz"
|
|
21 |
"Baz",
|
|
22 |
"color"
|
22 | 23 |
],
|
23 | 24 |
"properties": {
|
24 | 25 |
"some_base_property": {
|
|
115 | 116 |
"type": "string",
|
116 | 117 |
"foo": "bar",
|
117 | 118 |
"hello": "world"
|
|
119 |
},
|
|
120 |
"color": {
|
|
121 |
"enum": [
|
|
122 |
"red",
|
|
123 |
"green",
|
|
124 |
"blue"
|
|
125 |
],
|
|
126 |
"type": "string"
|
|
127 |
},
|
|
128 |
"rank": {
|
|
129 |
"enum": [
|
|
130 |
1,
|
|
131 |
2,
|
|
132 |
3
|
|
133 |
],
|
|
134 |
"type": "integer"
|
|
135 |
},
|
|
136 |
"mult": {
|
|
137 |
"enum": [
|
|
138 |
1,
|
|
139 |
1.5,
|
|
140 |
2
|
|
141 |
],
|
|
142 |
"type": "number"
|
118 | 143 |
}
|
119 | 144 |
},
|
120 | 145 |
"additionalProperties": false,
|
115 | 115 |
"type": "string",
|
116 | 116 |
"foo": "bar",
|
117 | 117 |
"hello": "world"
|
|
118 |
},
|
|
119 |
"color": {
|
|
120 |
"enum": [
|
|
121 |
"red",
|
|
122 |
"green",
|
|
123 |
"blue"
|
|
124 |
],
|
|
125 |
"type": "string"
|
|
126 |
},
|
|
127 |
"rank": {
|
|
128 |
"enum": [
|
|
129 |
1,
|
|
130 |
2,
|
|
131 |
3
|
|
132 |
],
|
|
133 |
"type": "integer"
|
|
134 |
},
|
|
135 |
"mult": {
|
|
136 |
"enum": [
|
|
137 |
1,
|
|
138 |
1.5,
|
|
139 |
2
|
|
140 |
],
|
|
141 |
"type": "number"
|
118 | 142 |
}
|
119 | 143 |
},
|
120 | 144 |
"additionalProperties": false,
|
76 | 76 |
|
77 | 77 |
// Test for "extras" support
|
78 | 78 |
Baz string `jsonschema_extras:"foo=bar,hello=world"`
|
|
79 |
|
|
80 |
// Tests for simple enum tags
|
|
81 |
Color string `json:"color" jsonschema:"enum=red,enum=green,enum=blue"`
|
|
82 |
Rank int `json:"rank,omitempty" jsonschema:"enum=1,enum=2,enum=3"`
|
|
83 |
Multiplier float64 `json:"mult,omitempty" jsonschema:"enum=1.0,enum=1.5,enum=2.0"`
|
79 | 84 |
}
|
80 | 85 |
|
81 | 86 |
type CustomTime time.Time
|