Skip to content

Commit

Permalink
Add: option to apply enum on array items
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmesa-jt committed Jun 12, 2024
1 parent 710fd2f commit c08b884
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Schema struct {
PatternProperties interface{} `json:"patternProperties,omitempty"`
Required []string `json:"required,omitempty"`
Items *Schema `json:"items,omitempty"`
ItemsEnum []any `json:"itemsEnum,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Expand Down Expand Up @@ -187,6 +188,11 @@ func processComment(schema *Schema, comment string) (isRequired bool) {
schema.Items = &Schema{
Type: value,
}
case "itemEnum":
if schema.Items == nil {
schema.Items = &Schema{}

Check warning on line 193 in pkg/schema.go

View check run for this annotation

Codecov / codecov/patch

pkg/schema.go#L193

Added line #L193 was not covered by tests
}
schema.Items.Enum = processList(value, false)
case "additionalProperties":
if v, err := strconv.ParseBool(value); err == nil {
schema.AdditionalProperties = &v
Expand Down
7 changes: 7 additions & 0 deletions pkg/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ func TestProcessComment(t *testing.T) {
expectedSchema: &Schema{MinItems: uint64Ptr(1), MaxItems: uint64Ptr(10), UniqueItems: true, Items: &Schema{Type: "boolean"}},
expectedRequired: false,
},
{
name: "Set array item enum",
schema: &Schema{},
comment: "# @schema minItems:1;maxItems:10;uniqueItems:true;item:boolean;itemEnum:[\"one\",\"two\"]",
expectedSchema: &Schema{MinItems: uint64Ptr(1), MaxItems: uint64Ptr(10), UniqueItems: true, Items: &Schema{Type: "boolean", Enum: []any{"one", "two"}}},
expectedRequired: false,
},
{
name: "Set object",
schema: &Schema{},
Expand Down

0 comments on commit c08b884

Please sign in to comment.