Skip to content

Commit 172fde8

Browse files
authored
feat(description): add required field description (#27)
It's helpful to a user to know what fields are required.
1 parent 0a90843 commit 172fde8

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package service
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/aep-dev/aep-lib-go/pkg/api"
8+
"github.com/aep-dev/aep-lib-go/pkg/openapi"
9+
)
10+
11+
func TestRequiredFieldDescription(t *testing.T) {
12+
resource := &api.Resource{
13+
Singular: "book",
14+
Plural: "books",
15+
Schema: &openapi.Schema{
16+
Properties: map[string]openapi.Schema{
17+
"title": {
18+
Type: "string",
19+
Description: "The title of the book",
20+
},
21+
"author": {
22+
Type: "string",
23+
Description: "The author of the book",
24+
},
25+
},
26+
Required: []string{"title"},
27+
},
28+
Methods: api.Methods{
29+
Create: &api.CreateMethod{},
30+
},
31+
}
32+
33+
args := []string{"create", "--help"}
34+
_, output, _ := ExecuteResourceCommand(resource, args)
35+
36+
if !strings.Contains(output, "The title of the book (required)") {
37+
t.Errorf("Expected description for required field 'title' to contain '(required)', but got:\n%s", output)
38+
}
39+
40+
if strings.Contains(output, "The author of the book (required)") {
41+
t.Errorf("Expected description for optional field 'author' NOT to contain '(required)', but got:\n%s", output)
42+
}
43+
}

internal/service/resource_definition.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ func addSchemaFlags(c *cobra.Command, schema openapi.Schema, args map[string]int
206206
if description == "" {
207207
description = fmt.Sprintf("The %v of the resource", name)
208208
}
209+
210+
// Check if the field is required
211+
isRequired := false
212+
for _, required := range schema.Required {
213+
if required == name {
214+
isRequired = true
215+
break
216+
}
217+
}
218+
if isRequired {
219+
description = fmt.Sprintf("%s (required)", description)
220+
}
221+
209222
if prop.ReadOnly {
210223
continue
211224
}

0 commit comments

Comments
 (0)