Codebase list golang-github-alecthomas-jsonschema / e803ab2
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 changed file(s) with 154 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
88
99 - Supports arbitrarily complex types, including `interface{}`, maps, slices, etc.
1010 - Supports json-schema features such as minLength, maxLength, pattern, format, etc.
11 - Supports simple string and numeric enums.
1112 - Supports custom property fields via the `jsonschema_extras` struct tag.
1213
1314 ## Example
2324 BirthDate time.Time `json:"birth_date,omitempty" jsonschema:"oneof_required=date"`
2425 YearOfBirth string `json:"year_of_birth,omitempty" jsonschema:"oneof_required=year"`
2526 Metadata interface{} `json:"metadata,omitempty" jsonschema:"oneof_type=string;array"`
27 FavColor string `json:"fav_color,omitempty" jsonschema:"enum=red,enum=green,enum=blue"`
2628 }
2729 ```
2830
8486 },
8587 "a": "b",
8688 "foo": "bar"
89 },
90 "fav_color": {
91 "type": "string",
92 "enum": [
93 "red",
94 "green",
95 "blue"
96 ]
8797 }
8898 },
8999 "additionalProperties": false,
2525 "TestFlag",
2626 "age",
2727 "email",
28 "Baz"
28 "Baz",
29 "color"
2930 ],
3031 "properties": {
3132 "some_base_property": {
122123 "type": "string",
123124 "foo": "bar",
124125 "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"
125150 }
126151 },
127152 "additionalProperties": true,
2525 "TestFlag",
2626 "age",
2727 "email",
28 "Baz"
28 "Baz",
29 "color"
2930 ],
3031 "properties": {
3132 "some_base_property": {
122123 "type": "string",
123124 "foo": "bar",
124125 "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"
125150 }
126151 },
127152 "additionalProperties": false,
1010 "TestFlag",
1111 "age",
1212 "email",
13 "Baz"
13 "Baz",
14 "color"
1415 ],
1516 "properties": {
1617 "some_base_property": {
107108 "type": "string",
108109 "foo": "bar",
109110 "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"
110135 }
111136 },
112137 "additionalProperties": false,
1818 "TestFlag",
1919 "age",
2020 "email",
21 "Baz"
21 "Baz",
22 "color"
2223 ],
2324 "properties": {
2425 "some_base_property": {
115116 "type": "string",
116117 "foo": "bar",
117118 "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"
118143 }
119144 },
120145 "additionalProperties": false,
115115 "type": "string",
116116 "foo": "bar",
117117 "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"
118142 }
119143 },
120144 "additionalProperties": false,
379379 Type: ty,
380380 })
381381 }
382 case "enum":
383 switch t.Type {
384 case "string":
385 t.Enum = append(t.Enum, val)
386 case "integer":
387 i, _ := strconv.Atoi(val)
388 t.Enum = append(t.Enum, i)
389 case "number":
390 f, _ := strconv.ParseFloat(val, 64)
391 t.Enum = append(t.Enum, f)
392 }
382393 }
383394 }
384395 }
7676
7777 // Test for "extras" support
7878 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"`
7984 }
8085
8186 type CustomTime time.Time