Skip to content

Commit 838e909

Browse files
committed
warn about untyped values
1 parent 6e106c7 commit 838e909

2 files changed

Lines changed: 56 additions & 20 deletions

File tree

cmd/helm-values/internal/generate.go

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,10 @@ func (g *Generator) Generate() (*jsonschema.Schema, error) {
4848
}
4949
s.Schema = JsonSchemaURI
5050

51-
s.WalkProperties(func(keyPath []*jsonschema.Schema, schema *jsonschema.Schema) {
52-
if !isDocumented(append(keyPath, schema)) {
53-
if schema.Title == "" {
54-
return
55-
}
56-
57-
keyValues := []string{}
58-
for _, k := range append(keyPath, schema) {
59-
if k.Title == "" {
60-
continue
61-
}
62-
keyValues = append(keyValues, k.Title)
63-
}
64-
g.logger.Warnf("undocumented value: %s", strings.Join(keyValues, "."))
65-
}
66-
})
51+
s.WalkProperties(
52+
g.warnUndocumentedValue,
53+
g.warnUntypedValue,
54+
)
6755

6856
return s, err
6957
}
@@ -75,7 +63,9 @@ func (g *Generator) buildScalarNode(key *yaml.Node, value *yaml.Node) (*jsonsche
7563
}
7664

7765
extraNodes := []*yaml.Node{}
78-
extraNodes = append(extraNodes, comment.KeyValueNodes("type", valueType)...)
66+
if valueType != "null" {
67+
extraNodes = append(extraNodes, comment.KeyValueNodes("type", valueType)...)
68+
}
7969
extraNodes = append(extraNodes, comment.KeyValueNodes("title", key.Value)...)
8070
extraNodes = append(extraNodes, comment.KeyValueNodes("default", value.Value)...)
8171

@@ -218,3 +208,41 @@ func isDocumented(schemaPath []*jsonschema.Schema) bool {
218208

219209
return false
220210
}
211+
212+
func (g *Generator) warnUndocumentedValue(keyPath []*jsonschema.Schema, schema *jsonschema.Schema) {
213+
if !isDocumented(append(keyPath, schema)) {
214+
if schema.Title == "" {
215+
return
216+
}
217+
218+
keyValues := []string{}
219+
for _, k := range append(keyPath, schema) {
220+
if k.Title == "" {
221+
continue
222+
}
223+
keyValues = append(keyValues, k.Title)
224+
}
225+
226+
g.logger.Warnf("value is undocumented: %s", strings.Join(keyValues, "."))
227+
}
228+
}
229+
230+
func (g *Generator) warnUntypedValue(keyPath []*jsonschema.Schema, schema *jsonschema.Schema) {
231+
if schema.Title == "" {
232+
return
233+
}
234+
235+
if schema.Type != "" {
236+
return
237+
}
238+
239+
keyValues := []string{}
240+
for _, k := range append(keyPath, schema) {
241+
if k.Title == "" {
242+
continue
243+
}
244+
keyValues = append(keyValues, k.Title)
245+
}
246+
247+
g.logger.Warnf("value has no type: %s", strings.Join(keyValues, "."))
248+
}

cmd/helm-values/internal/jsonschema/schema.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,18 @@ type Schema struct {
7979
// Extensions map[string]ExtSchema `json:"extensions,omitempty"`
8080
}
8181

82-
func (s *Schema) WalkProperties(fn func(keyPath []*Schema, schema *Schema), keyPath ...*Schema) {
83-
fn(keyPath, s)
82+
type NodeInspector func(keyPath []*Schema, schema *Schema)
83+
84+
func (s *Schema) WalkProperties(fn ...NodeInspector) {
85+
s.walkProperties(fn)
86+
}
87+
88+
func (s *Schema) walkProperties(fns []NodeInspector, keyPath ...*Schema) {
89+
for _, fn := range fns {
90+
fn(keyPath, s)
91+
}
8492

8593
for _, k := range s.Properties {
86-
k.WalkProperties(fn, append(keyPath, s)...)
94+
k.walkProperties(fns, append(keyPath, s)...)
8795
}
8896
}

0 commit comments

Comments
 (0)